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]
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]
My URL looks something like this:
example.com/index.php?redirected=1
in my .htaccess-file, I want to remove the last part of my url.
I wrote that:
RewriteCond %{HTTP_HOST} ^example.com\/index.php\?redirected=1$
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
But for some reason, it isn't working. It is right after the line:
RewriteEngine on
RewriteBase /
I found similar questions, but that didn't do the trick.
Thanks
With this code in the .htaccess-file, it redirects me to: www.example.com/index.php
RewriteCond %{THE_REQUEST} \ /+(.*)\?redirected=1&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R]
When i remove the %2 (which i thought was the index.php-part, it'll result in infinte redirects.
Update:
I updated the regexp like this:
RewriteCond %{THE_REQUEST} \ /+(.*)\?redirected=1&?([^\ ]*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
still the "index.php" in the end
Try this instead:
RewriteCond %{THE_REQUEST} \ /+(.*)\?redirect=1&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R]
The %{HTTP_HOST} variable is only the hostname, it contains no path or query string in it.
You can use this rule for stripping query string and as well as removing index.php:
RewriteCond %{THE_REQUEST} /index\.php\?redirected=1[&\s] [NC]
RewriteRule ^(.*?)index\.php$ /$1? [L,R=302,NC,NE]
We want to change links "www.example.com/page/login/" to "www.example.com/sayfa/giris". I tried this: RewriteRule ^page/^(.+[^/])$/ /sayfa/^(.+[^/])$ [L] But nothing is change. Is there any error?
Thank you very much for your response #geert3 and #anubhava. But all trying failed. Here is my .htaccess content:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteRule ^sayfa/giris/?$ /page/login [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Make sure there is not .htaccess in /sayfa/ folder.
I think you need to remove the second ^ and add an initial slash after the first ^. The slash after $ must be removed.
^/page/(.+[^/])$
^ at the start of an expression means "beginning of line" (i.e. in your case you want /page in front of the path). After a square bracket it means "none of the listed characters" (i.e. in your case no slash). In other places, it makes no sense.
$ means "end of line" so it makes no sense to add stuff after it.
Also as #Croises noted, you can't have "matches" on the right. Right side is your target URL, so there you can either re-use matches you made on the left side, use environment variables or hardcoded text, but no new matches. So no ^, [], $ etc.
So for instance the whole rule would become:
^/page/(.+[^/])$ /sayfa/giris [L]
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]
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.