How do i create virtual subdomain using htaccess? - subdomain

I would like to create virtual sub domains for clients who will have websites with us but the websites will be served from a single url in reality for example;
main site:
domain.com/post/20/Posttitle
domain.com/index.php?r=post/20/Posttitle
sub site:
mysub.domain.com
domain.com/index.php?s=mysub
OR
mysub.domain.com/post/20/Posttitle
domain.com/index.php?s=mysub&r=post/20/Posttitle
OR
mysub.domain.com/Cat/2/cattitle
domain.com/index.php?s=mysub&r=Cat/2/cattitle
OR
mysub.domain.com/{OrderURL}
domain.com/index.php?s=mysub&r={OrderURL}
....
** In a general example: Blog system
How do i do this?
Also my setup is with PHP{Something like the MVC} and cPanel / WHM server.
Note: The design on localhost
Here is the htaccess code i have tried so far (does not work properly);
RewriteCond %{HTTP_HOST} ^(http://|https://)?(www\.)?(.*\.)?(.*)$
RewriteRule (http://|https://)?(www\.)?(.*\.)?(.*)$ index.php?s=$3&r=$4 [L,NC,QSA]
RewriteRule ^(.*)$ index.php?r=$1 [L,NC,QSA]

Related

Self Hosted Website www redirect to non www

I am self hosting a website on a Synology NAS. I have set up an SSL certificate, set up the DNS records on Google Domains. If I enter the website, andrewr.ca, into a browser it works fine. If I enter www.andrewr.ca it does not work and the SSL is not valid. I am trying to figure out how to fix this issue but get stuck every time. I have a .htaccess file that looks like this:
And my Google Domains account looks like this. [enter image description
My SSL cert looks like this:
Could someone please point me in the right direction here. I just want to be able to enter www.andrewr.ca and it works the exact same as if I enter andrewr.ca
I have tried changing the CNAME, changing the .htaccess and also adding a different SSL certificate but had no luck with anything I try so far.
This honestly probably has two separated answers out there for you to find, but I am posting this just because it's a solution we use currently in our setup to rewrite non https to https AND non www to www in one swoop. These are the rewrite directives in my production .htaccess
#SSL = 1
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^/?(.*)$ https://example.com/$1 [R=301,L]
# Redirect www and http to https - non-www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
This is not for everyone and ONLY works if you have a single domain in your setup -- Which the top two lines of the htacces file there are put there to deal with multiple domains on the single files set to canonicalize the domain .. Once you have multiple domains in there .. You WILL NEED a separated SSL for each domain, AND the www for it to work .. But as for a SINGLE domain, this will forward to https - non www without the need for a secondary SSL on the www

Redirecting multiple duplicate URLs to main URL using .htaccess

I developed a website and from SEO point of view, the homepage of a website is available under three or more different URLs which accordingly poses a risk of duplicate content, since the same content can be accessed via different URLs.
for eg.
my website is opening with below URL
https://www.insider.in/index.php
https://www.insider.in/
https://insider.in/
Exact URL which is needed be like
https://www.insider.in/
for achieving the same i am using an .htaccess file with code given below:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.(php|html?)[#?]?
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ https://insider.in/$1[R=301,L]
I tried to redirect all duplicate URLs to one URL using the above .htaccess file code but it is not working, also i enabled all the file permissions in FileZilla but again no success.
Any help or reference will be appreciated, thankyou!
note- website is hosted in cloud server using FileZilla
for Reference I am sharing the screenshots
Image of development folder for your reference
Image of .htaccess rule which i have used
You can use the rules below to fix duplicate content issues on your site :
RewriteEngine on
#Redirect non-www domain to www
RewriteCond %{HTTP_HOST} ^insider\.in$ [NC]
RewriteRule ^ https://www.insider.in%{REQUEST_URI} [NE,L,R=301]
#Redirect /index.php to /
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule ^ https://www.insider.in/ [L,R=301]
Remember to put theses rules at the top of your htaccess.

How do I redirect "website/page" to "website/page.html" when people type it in browser (pretty-url)

I recently started using a static site with godaddy.com without a cms. It's a basic website I uploaded and is only html/js/css.
When people type "website.com/page" they get 404 not found.
When they type "website.com/page.html" they get the correct page.
How can I fix my site so users can simply type "website/page" and not be forced to type "website/page.html" without using php?
SOLUTION
After researching the information about .htaccess files provided by #Gijsberts (thank you), I did the following:
Created a .htaccess file on server.
Added the following code:
# Remove the .html extension from html files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
Most CMS systems have a .htaccess file what handles the redirect and the 404 errors. In CMS system they are most called "Pretty-urls". Read the links below to set up those pretty urls yourself with a static web page without a cms.
https://mediatemple.net/community/products/grid/204643080/how-do-i-redirect-my-site-using-a-htaccess-file
http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
You normally can do this with your Webserver config. In Apache you have this enabled automatically. In nginx you should have a look at this tutorial.
If you are using caddy you have to config your caddyfile like
ext .html .htm .php
if you are using microsoft iis have a look at microsofts tutorial.
Let me know if you are using something else or need more help.
EDIT
If you are using a webspace you may are not able to change the config of the webserver. you now can use .htaccess. create a .htaccess file and write down something like
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

(HTTP) Hide page extension in the URL

I'm making a web page using my Raspberry PI and Apache. I made the page chat.html.
To connect to it I have to write in Chrome "http://192.168.1.39/chat.php". How can I hide the '.php'(or anything else) from page URL, so when I connect to "http://192.168.1.39/chat" directly it doesn't give Error 404?
Thank you in advance.
Use URL rewriting by utilizing .htaccess. Basically .htaccess is a file that let's you alter the config of a webserver like Apache.
In your web site root, create a filed called .htaccess.
Paste this in it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
*.php files can now be accessed the way you describe. For more info on htaccess and url rewriting, please reference http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

What is the proper .htaccess for blocking another website assigned to my dedicated IP

Here is my scenario: I have a website, that I own, and another website, which someone I don't know owns, is also assigned to that same IP address. When I view their website it is basically displaying all of the content from my website. This URL is #1 in certain keywords I'm using any my website is several pages deep in google. I can also log into the administrator portion of the website through the other URL that I don't own, which worries me from a liability stand point. I contacted my hosting provider and after about an hour and nothing to show for it here I am.
What I want to do is block the other website in .htaccess OR completely redirect all pages from that URL to my URL...
What is the best redirect method or code in this instance?
What I currently have still in my .htaccess allows the index page to be loaded on the remote website but all of the images and css loaded by that index page respond with a 403 forbidden. Problem with that is it is still loading my index page's meta description, keywords, html, and styles which is messing with my search engine optimization attempts.
I want the index page to not even load resulting in a forbidden error or a complete redirect including the base index page.
I also want to redirect any traffic that doesn't include the www before my domain to the www version and not result in a redirection loop.
Any help is greatly appreciated.
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC]
RewriteRule .* - [F]
RewriteCond %{HTTP_HOST} ^otherdomain\.com [NC]
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www\.mydomain\.com [R=301,L]
Use this code:
# block other domain
RewriteCond %{HTTP_HOST} ^(www\.)?otherdomain\.com$ [NC]
RewriteRule ^ - [F]
# put www before host name
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]