Mobile app version of vmapp.org
Login or Join
Cugini213

: Redirect URL with multiple trailing dots and trailing slashes using .htaccess I need to fire 301 redirection from example.com... to example.com and example.com////// to example.com Here is my code:

@Cugini213

Posted in: #Htaccess #Redirects #TrailingSlash

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 ^(.*)$ example.com/ [R=301,L]

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

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

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Cugini213

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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 (.*) example.com/ [R=301,L]

# Remove multiple slashes at the start of the URL-path
RewriteCond %{THE_REQUEST} //+
RewriteRule (.*) / [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 ( 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:


How can URLs have a dot . at the end, e.g. bla.de.? How should websites handle hostname with trailing dot?

10% popularity Vote Up Vote Down


 

@BetL925

maybe this one:

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme