: Mod_rewrite prevent repeating backreference I am trying to match a single reference in a URL to pass to a script, while retaining the rest as the path to the script. For example: #/scripts/paramater/foo/bar.php
I am trying to match a single reference in a URL to pass to a script, while retaining the rest as the path to the script. For example:
#/scripts/paramater/foo/bar.php should call
#/scripts/foo/bar.php?id=parameter
RewriteRule ^scripts/([^/]+)/(.*)$ scripts/?id= [L,QSA]
However my backreference is matching multiple times. only contains "bar.php" and is somehow repeating for "parameter" and "foo".
How can I prevent the backreference from repeating itself?
Edit:
I've narrowed down the problem somewhat by fiddling with the second part of the rewrite rule.
#This doesn't work, as above #Outputs : scripts/bar.php?id=foo?id=parameter
RewriteRule ^scripts/([^/]+)/(.*)$ scripts/?id= [L,QSA]
#This works
#Outputs: foo/bar.php?id=parameter
RewriteRule ^scripts/([^/]+)/(.*)$ ?project_slug= [L,QSA]
Which confuses me even more! Any help appreciated
More posts by @Angie530
1 Comments
Sorted by latest first Latest Oldest Best
Easy: add condition that will prevent execution of rewrite rule on already rewritten URL.
I see 2 main approaches (based on your URL examples):
1. Will prevent rewriting if URL was already rewritten by the same rule:
RewriteCond %{QUERY_STRING} !^project_slug=
RewriteRule ^scripts/([^/]+)/(.*)$ scripts/?project_slug= [L,QSA]
On initial rewrite iteration URL will be rewritten and query string will have project_slug= at the start of the string. On next iteration the condition will not allow rewriting because query string already starts with project_slug=.
Of course, this approach will not work if original URL (before rewriting) has query string that starts with project_slug= (e.g. /scripts/parameter/foo/bar.php?project_slug=hello) .. which should not happen under normal circumstances (unless someone knows how rewrite rule works and is messing around on purpose).
2. Allow rewrite execution ONLY on first iteration (regardless of which rule has rewritten this URL):
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^scripts/([^/]+)/(.*)$ scripts/?project_slug= [L,QSA]
Unfortunately the variable used here %{ENV:REDIRECT_STATUS} is not populated on every Apache build/setup. Therefore it may work fine on your setup but may not on another hosting. Unfortunately I cannot tell you how to determine (configure your Apache) if it will work without executing some sort of rewrite test.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.