1

I am working on a site where I have many short URLs which is redirected to a bigger version of URL. The idea is that the short URLs are distributed offline and users generally remembers them and types in the browser address bar. As example: http://www.example.com/abc should redirect to http://www.example.com/higher_education/companion/politics/abc.html
Also there are some external links as well. like: http://www.example.com/google will redirect user to https://www.google.com
EDIT - There is another scenario where http://www.example.com/elementary/def.html will be redirected to http://www.example.com/elementary/xyz.html
However, we now want to achieve this with RewriteMap. The code that we've written for that is as below:

<IfModule rewrite_module>
RewriteEngine on
RewriteMap custommap txt:/var/www/vhosts/example.com/httpdocs/map.txt
#RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule "^(.*)$"  "${custommap:$0}" [R,L,NC]
</IfModule>

The map.txt contains:

abc http://www.example.com/higher_education/companion/politics/abc.html
google https://www.google.com

The problem is .htaccss file contains also some RewriteRule.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)\.html$ /index.php?sid=$1&ssid=$2 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /index.php?sid=$1 [L,NC,QSA]

So any request comes with .html will be served by index.php as standard CMS feature. However, now the code goes into redirect loop. If someone shares some light in order to mitigate this issue then that would be great. The Apache version is 2.2 so I can't use any IF-ELSE in that.

Regards,
Aneek

1 Answers1

1

You should put some restrictions on the rule serving URLs from RewriteMap:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${custommap:$0} !^$
RewriteRule ^[^.]+$ ${custommap:$0} [R=301,L,NE]

This will ensure only non-files and non-directories with no DOT in URI will be handled by custommap.

More importantly it will also check existence of a mapping in custommap before redirecting.

Make sure you clear your browser cache before testing this change.

anubhava
  • 761,203
  • 64
  • 569
  • 643