1

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

5
  • Just add RewriteCond %{REQUEST_FILENAME} !-f before your RewriteRule to skip all files to rewrite Commented Apr 11, 2022 at 20:05
  • so in this case would it be RewriteCond %{url.php} !-f ? what is the exact syntax - and can url.php be relative to the current directory as I wrote it above? if you have an actual answer please post it as an answer so I can upvote and give you credit. Commented Apr 11, 2022 at 20:17
  • You need to use RewriteCond %{REQUEST_FILENAME} !-f literally as is before RewriteRule line Commented Apr 11, 2022 at 20:36
  • ok so that will do all 'files' - let me see if that works... still so you get credit please write your answer so I can upvote and give you points. this is the answer I was looking for. Commented Apr 11, 2022 at 20:42
  • Unfortunately that did not work we tried the URL in a private window and what happened was it did not capture the short extension example.com/url.php?i=url.php it passed the url into the url parameter after 1 redirect. What it needs to do is pass the URL once example.com/abc or example.com/abc/ - we currently use 404 redirect on php however our server admins informed that this is not a best practice. Your help is appreciated. Commented Apr 11, 2022 at 23:56

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.