mod_rewrite rules for my site - apache

I have these rules on my site:
RewriteCond $1 !^(support|about-us|profile|support|features|videos|terms-of-service|about-us|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ https://$1.example.com/$2 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(support|about-us|profile|support|features|newserver|videos|about-us|terms-of-service|signup|component|includes|media|cgi-bin|templates|xmlrpc|language|modules|libraries|plugins|administrator|images(/.*))$ https://example.com/$1 [R=301,L]
# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ https://%1%3example.com/$1 [R=301,L]
I added these below to redirect these types: https://example.com/firstnamelastname?mv=0 etc
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ https://$1.example.com/? [L,R]
However I cannot get these to redirect https://example.com/firstnamelastname
If i remove the Query string from the last 3 lines then If i would click on features link on my website It would redirect to https://features.example.com which is what the rules at the top are preventing.
Can anyone assist with this.

Related

Minimal .htaccess code for a set of redirection rules

I want to achieve the following behavior with the least number of rules:
Enforce HTTPS
Redirect www to non-www
Remove .php and .html extensions from all URLs
Redirect example.com/index.php and example.com/index to example.com
Redirect example.com/static/*.html to example.com/*
Remove trailing slashes
Right now I use the following rules:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ /$1.html
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
which don't include a rule for the static directory as I don't have introduced it yet. I plan to do it for caching reasons now.

HTTPS redirect from non www to non www

I have a non-WWW domain and when I try to set up redirect to HTTPS, redirect from http://www.examle.com to https://www.example.com works for me, but http://example.com does not work on https://example.com. The current htaccess is:
RewriteEngine On
RewriteCond %{REQUEST_URI} .*index\.html/?$ [NC]
RewriteRule ^(.*)index\.html/?$ https://$1 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond $1 !index\.php
RewriteCond $1 !^assets
RewriteCond $1 !^robots.txt
RewriteCond $1 !^sitemap.xml
RewriteRule ^(.*)$ index.php [L]
Can you advise what I'm doing wrong? Thank you
UPDATE:
Current .htaccess
RewriteEngine On
RewriteCond %{REQUEST_SCHEME} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{REQUEST_URI} .*index\.html/?$ [NC]
RewriteRule ^(.*)index\.html/?$ https://$1 [R=301,L,NC]
RewriteCond $1 !index\.php
RewriteCond $1 !^assets
RewriteCond $1 !^robots.txt
RewriteCond $1 !^sitemap.xml
RewriteRule ^(.*)$ index.php [L]
You are using this rule for https redirect:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
This has first condition as %{HTTP_HOST} ^www\. which means it will only match host names starting with www. thus you will not get redirect for http://example.com/.
You may use this redirect rule instead:
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
This will perform following redirects:
http://example.com/ => https://example.com/
http://www.example.com/ => https://www.example.com/
Make sure you clear your browser cache before testing this.

.htaccess force to SSL and remove filename extensions

I am trying to rewrite my URL at the moment using .htaccess basically I want to force all connections to https:// and also remove any trailing .html extensions.
Here is what I have so far,
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST}$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This forces the user to use https but it does not remove the .html from the URL where am I going wrong?
You need an additional rule to strip off .html from URLs. Moreover you can combine that rule and www and https rules into one to avoid mumtiple 301 redirects:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{THE_REQUEST} \.html[\s?] [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)(?:\.html)?$ https://%1/$1 [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

.htaccess rules causing internal server error yet they look right

My server's. htaccess rules look like this:
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]
But for some reason none it causes a server internal error, I'm not sure if they are all 100% correct either. Like the last rule, should that too use ^(.*)$ at the start and $1 at the end
Same for the flags, not too sure if they are all correct.
Basically I am trying to make:
testing.examplewebsite.co.uk and www.testing.examplewebsite.co.uk go to https://testing.examplewebsite.com
test.examplewebsite.co.uk and www.test.examplewebsite.co.uk go to https://test.examplewebsite.com
Some other subdomains like above
examplewebsite.co.uk and www.examplewebsite.co.uk go to https://examplewebsite.com
Make all used domains, for example stackoverflow.examplewebsite.co.uk and www.stackoverflow.examplewebsite.co.uk both be sent to https://examplewebsite.com
You have a typo in all of your conditions. the 2nd conditions in your lines is missing a . before uk. ^testing.examplewebsite.co.uk$ [NC]
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]

Some Problem with htaccess!

How can I redirect my users from example.com / or www.example.com to new.example.com
but I dont want to redirect some specific urls like:
www.example.com/test.php
api.example.com/testo.php
www.example.com/forum/index.php
I did:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]
but what is the rules for urls?
One way to do this is to create additional rules earlier in the .htaccess file for the specific URLs that just redirect to themselves (internally, not using a 3XX response), and then use the L flag so no later rules are processed for those URLs.
For example:
RewriteEngine on
# don't redirect these
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/test.php)$ \1 [L]
RewriteCond %{HTTP_HOST} ^api.example.com$
RewriteRule ^(/testo.php)$ \1 [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/forum/index.php)$ \1 [L]
# redirect these
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]
I'm not sure if your "don't redirect" requirements included the hostname. If not, then just remove the RewriteCond lines.