I already have a rule set up to remove www from my urls and redirect them...
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*) http://'%'1$1 [R=301,L]
*note-I had to put quotes around the percent sign to post this message, the actual rule does not contain them.
I now want to also strip off any trailing /
How would I do this?
Add an extra rule:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [L,R=301]
Related
I want to modify an .htaccess file to do 2 things:
Force the addition of a slash at the end of each URL
Replace the excess slashes (2 and more) by 1 single slash.
Here is a set of URLs and their expected result:
https://www.mywebsite.io/fr > https://www.mywebsite.io/fr/
https://www.mywebsite.io/fr/register > https://www.mywebsite.io/fr/register/
https://www.mywebsite.io/fr/register// > https://www.mywebsite.io/fr/register/
https://www.mywebsite.io/fr////register > https://www.mywebsite.io/fr/register/
https://www.mywebsite.io/fr/register///other////// > https://www.mywebsite.io/fr/register/other/
I manage to do the first step which is to force a final / but I cannot replace the extra /.
Can you help me with this problem?
Here is the content of my .htaccess file
Options +FollowSymlinks
RewriteEngine On
## Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [L,R]
## Force slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R]
## Remove repeated slashs
#RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$ [N]
#RewriteRule (.*) %1 [R,L]
RewriteRule (.*)/{2,}(.*) $1/$2 [N]
RewriteRule ^(fr|en){1}(/?)$ content/index.php?lang=$1 [L]
RewriteRule ^(fr|en){1}(/){1}(registration){1}(/?)$ content/registration.php?lang=$1 [L]
Have it like this: (see comments inline)
Options +FollowSymlinks
RewriteEngine On
## add https and trailing slash in same rule
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*?)/?$ https://%{HTTP_HOST}/$1/ [R=301,L,NE]
## Remove repeated slashs
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=301,L,NE]
RewriteRule ^(fr|en)/?$ content/index.php?lang=$1 [L,QSA]
RewriteRule ^(fr|en)/registration/?$ content/registration.php?lang=$1 [L,QSA]
With your shown samples, could you please try following. I have also fixed your small issues of missing flags too in your question. Please make sure you clear your browser cache before testing your URLs.
Options +FollowSymlinks
RewriteEngine On
## Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [L,R]
## make slashes to 1 at last or uri, in between slashes will be automatically moved to single slash.
RewriteRule ^([^/]*)(?:/+)$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^(fr|en)/?$ content/index.php?lang=$1 [NC,L]
RewriteRule ^(fr|en)/(registration)(/?)$ content/$2.php?lang=$1 [NC,L]
I would like to force my urls to https, www and no trailing slashes.
Since I have multiple domains that point to this page, a static domain in there is no option.
This is what i have so far:
# force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
This works quite well, except for the case:
https://www.example.com//
which redirects to:
https://www.www.example.com/
Any suggestions on how I can make this work?
Also from a redirects perspective I'm not really sure if this will make 3 redirects in the worst case and can somehow be improved to only one redirect?
Thanks in advance!
In the meantime I found something else which seems to make more sense
# force https and www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Now the only remaining problems are the following two requests:
http://www.example.com
which redirects to https://www.www.example.com
and
https://www.example.com//
which does not remove the multiple slashes.
any ideas? would really appreciate help!
I can remove multiple slashes anywhere in URL using:
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
But it doesn't work for multiple slashes after the domain
I have tried
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
from: remove multiple trailing slashes mod_rewrite
and
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ (.*)//([^\ ]*)
RewriteRule ^ %2/%3 [R=301,L]
from: Remove trailing slash after domain
both produce the expected rewirting when going from
domain.com/////hello
to
domain.com/hello
but from
domain.com/////héllo
the result is encoded
domain.com/h%25c%25allo
How to prevent accented characters to get encoded when removing multiple slashes after domain ?
EDIT: pear to anubhava's answer
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*) [NC]
RewriteRule ^ %1/%2 [R=301,L,NE]
The accented character is protected and trimed with succes with more than to repeated slashes
domain.com////////héllo
but not with only 2
domain.com//héllo
This rule should work for you:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)/+(/[^\s]+) [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
This will do these redirections:
/////help => /help
/////héllo/////abc/////123 => /héllo/abc/123
anubhava's answer is the shortest but alternatively this one is also working:
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L,NE]
I would like to 301 redirect the following URL
www.domain.com/Gallery2/v/South/Actress/Ritu+Kaur+Actress+Photos
to
www.domain.com/gallery/ritu-kaur.html
I tried the following, but not worked,
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^Gallery2\/v\/South\/Actress\/Ritu\+Kaur\+Actress\+Photos\/$ "http\:\/\/www\.indiancinemagallery\.com\/gallery\/ritu\-kaur\.html" [R=301,L]
Please advise
Place this rule in your Gallery2/.htaccess before any other rewrite rule
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^v/South/Actress/Ritu\+Kaur\+Actress\+Photos/?$ http://www.indiancinemagallery.com/gallery/ritu-kaur.html [R=301,L,NC]
The + signs will probably get canonicalized to spaces, try replacing them with spaces instead:
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^Gallery2/v/South/Actress/Ritu\ Kaur\ Actress\ Photos/$ http://www.indiancinemagallery.com/gallery/ritu-kaur.html [L,R=301]
The URI is decoded before being put through matching against regex in RewriteRule's, so unless your url had encoded + signs, then they'll get decoded to spaces instead.
I'd like to redirect web requests from example.org to www.example.org. I think I have the right rule, but it is adding two extra forward slashes:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
The above would take me from example.org to www.example.org//
Is there a way to strip those trailing slashes? I tried the following variant, which strips those slashes but causes a redirect loop:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
How might I solve this?
Try adding RewriteBase / right after RewriteEngine on.
If that doesn't do it, change your rule to RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L] so you don't capture the initial /.