RewriteRule ^([^/]*)$ /index.php?shortened_url=$1 [L]
This will result in a rewrite loop because /index.php?shortened_url=value
will get further rewritten to /index.php?shortened_url=index.php
(again and again).
One way to prevent this rewrite loop is to only rewrite when there is no query string (providing you aren't using the query string for other purposes on this URL). For example:
RewriteCond %{QUERY_STRING) ^$
RewriteRule ^([^/]*)$ /index.php?shortened_url=$1 [L]
Or, exclude dots from the RewriteRule
pattern. For example:
RewriteRule ^([^/.]*)$ /index.php?shortened_url=$1 [L]
The important thing to note here is that the L
(last
) flag does not stop all processing when used in per-directory .htaccess
files. It simply stops the current round of processing. The rewriting process effectively starts over until the URL passes through unchanged. You need to prevent the rewritten URL from being further rewritten.
that will change:
https://example.com/index.php?shortened_url=value
to
https://example.com/value
bocian85 certainly has a point regarding your description, as your code is doing the complete opposite. Your code rewrites https://example.com/value
to https://example.com/index.php?shortened_url=value
. (Presumably you have already changed the URLs in your application to be of the form https://example.com/value
?)
However, the code looks like it's doing the correct thing, so I think it's just your description that is back to front. (?) (Curious... people often describe this as you have done - in the reverse order - as if they are describing the net result in the application, rather than what the directive actually does.)