简体   繁体   中英

Mod_rewrite in .htaccess - forward anything that starts with index.php to ____

UPDATE: This works:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule (.+) /index.cfm?event=checkuri&uri=%1 [QSA]

Some background...

So we already have a catchall redirect in our .htaccess file which is this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index.cfm?event=checkuri&uri=$1 

This ties into a database table that checks the URI for a match. so if we just moved a site that used to have this page:

/some-awesome-article.html

Onto our system, and the new address is

/awesome-article/12442

and someone tried to access the old URI, our system would check for this, find a match, and forward them to the new home: /awesome-article/12442


This system works awesome, with one exception. If the URI is something like /index.php?id=123412 then the whole system falls apart. In fact /index.php/whatever won't work either.

Everything else works except for this. We do not use PHP for our web application (although support says its in an admin console on the server somewhere).


So basically what I need is if index.php is detected anywhere it will forward the URI to our existing system:

How can i modify this to fix it?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index.cfm?event=checkuri&uri=$1 

Try changing your code to:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index.cfm?event=checkuri&uri=$1 [L,QSA]

QSA is for Query String Append that will make sure to append existing query parameters with the new ones.

Based on your comments, it sounds like you need to use the Query String Append QSA flag on your rule like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.cfm?event=checkuri&uri=$1 [QSA,L]

In your example case the rewrite would look like:

/index.cfm?event=checkuri&uri=index.php&id=123412

Rewriting with mod_rewrite does not work on the full URL. In fact, the regex in the RewriteRule does only get the path and file, but not the query string. And so the backreference $1 will only contain "index.php" and nothing else.

Additionally, the RewriteRule does change the query string because there is one in the target pattern. Because the flag [QSA] (query string append) is not present, the query string of the original request gets replaced instead of appended. So the query string is gone after this rewriting.

This would be a lot easier if you wouldn't mess with the query string. The easiest way of rewriting any url that is not an existing file would be if the second line would be simply RewriteRule (.+) /index.cfm - you could then get all info about the current request, including query string, path and file, in the script.

So now you'd have to fiddle with the query string. Adding [QSA] will pass the query string to your script and you'd have to detect what's inside. This will work only if you do not expect the query string to contain parameters named "event" and "uri" - these will be overwritten by your rewriting. If you need to add the original query string to the URL, it's a bit more complicated, because the string needs to be url-encoded.

Here's how to do that.

Sven was very close so I'm giving him the check

This ended up working perfectly:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule (.+) /index.cfm?event=checkuri&uri=%1 [QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM