Home > OS >  .htaccess GET from query string (exception)
.htaccess GET from query string (exception)

Time:04-14

I have a rewrite rule that works perfectly across domains but creates an endless loop on the same domain.

Allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ url.php?i=$1 [R=301,L]

the goal is to create an alternative to the 404 error that goes correctly to a page in a link shortening application.

for example, example.com/abc needs to redirect to example.com/url.php?i=abc

i.e. it fetches the 'abc' from after the '/'

however as long as url.php is hosted on xyz.com it redirects once and ends up with

example.com/url.php?i=url.php instead of the value "abc"

What is the best way to fix this rewrite rule so it fetches 'abc' once and passes it to the correct url.php?

thank you

CodePudding user response:

You need to remove R=301 flag as you don't want to expose your internal handling to outside clients. To make it internal you must not use R flag.

As mentioned earlier you need to use RewriteCond to skip files and directories being rewritten.

Suggest code:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ url.php?i=$1 [QSA,L]

QSA flag is for query string append.

Make sure to completely clear your browser cache before testing this change.

  • Related