3

I need to fire 301 redirection
from example.com... to example.com
and example.com////// to example.com

Here is my code:

RewriteCond %{THE_REQUEST} \ /([^\?\ .]*)\.(?:\?|\ |$)
RewriteRule ^ /%1 [L,R=301]

It does not work.

And here is my .htaccess file:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

RewriteCond %{THE_REQUEST} \ /([^\?\ .]*)\.(?:\?|\ |$)
RewriteRule ^ /%1 [L,R=301]
  • Do you mean redirect URLs with any number of dots and slashes, regardless of whether they contain filenames between them, just to your root domain? Does that include URLs ending or containing filenames with certain file extensions, or all file extensions? It might help to explain what you're trying to accomplish by explaining why you want to do this. – dan Dec 21 '16 at 4:40
1

The dot(s) at the end of the hostname (ie. a fully qualified domain) will appear in the HTTP_HOST server variable (not THE_REQUEST) and the multiple slashes at the start of the URL-path will be present in the THE_REQUEST variable (like you are using), so you will need at least two rules. Try something like:

# Remove trailing dot(s) on the hostname
RewriteCond %{HTTP_HOST} \.$
RewriteRule (.*) https://example.com/$1 [R=301,L]

# Remove multiple slashes at the start of the URL-path
RewriteCond %{THE_REQUEST} \ //+
RewriteRule (.*) /$1 [R=301,L]

The RewriteCond directive simply checks for multiple slashes at the start of the URL-path (after the first space in THE_REQUEST variable). The redirect then relies on the fact that Apache automatically truncates multiple slashes in the URL-path that is matched by the RewriteRule pattern and uses this ($1 backreference) in the substitution.

NB: Make sure you clear your browser cache before testing as any erroneous 301s are likely to have been cached.

Aside: I think a hostname with more than one trailing dot is strictly invalid - it will fail to resolve and never actually reach your server. (?) If the request is being made from a browser then the browser will probably truncate the multiple trailing dots - leaving just one (ie. fully qualified) - before making the request.

Reference:

0

maybe this one:

<IfModule mod_rewrite.c>
  RewriteCond %{THE_REQUEST} //|..
  RewriteRule ^.*$ /$0 [R=301,L,NE]
</IfModule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.