问题
I'm literally at the end of my tether with this. I've researched many other questions and answers on stackoverflow but still can't find the solution i need. I'm starting to think what i want to do is not possible.
So... the problem is this. I want to turn the following: e.g.
www.mydomain.com/visa-information/country.php?country={COUNTRY-NAME}&passport={PASSPORT-NAME}
To the following pretty url:
www.mydomain.com/visa-information/{COUNTRY-NAME}-visa-for-{PASSPORT-NAME}-citizens/
I have a partially successful rule in my htacces file as so:
RewriteRule ^/visa-information/([A-Za-z-]+)-visa-for-([A-Za-z-]+)-citizens/?$ visa-information/country.php?country=$1&passport=$2 [NC]
which works fine and does what i want if i enter the url into the browser address bar, but the real problem i'm having is getting it to re-direct to the pretty url via a form i have on pretty much every page of the site.
I've tried various re-direct rules like the one below:
RewriteCond %{QUERY_STRING} country=([A-Za-z-]+)&passport=([A-Za-z-]+) [NC]
RewriteRule visa-information/country.php visa-information/%1-visa-for-%2-citizens/? [R,NC,L]
But no luck. I've also tried adding the QSA flag to the above re-direct rule, but it just ends up with an endless loop.
I have tried using a location php re-direct header at the top of the country.php page to re-direct after form submission like so:
if(isset($_GET['country']) && isset($_GET['passport'])) {
header("Location: " . $dir . "/visa-information/" . $currentCountry . "-visa-for-" . $currentPassport . "-citizens/");
exit();
}
I was expecting the above to work like entering the pretty url directly into the browser works, but it doesn't, just gives me a 404 error.
Any help is greatly appreciated.
Thanks
Jordash
EDIT
My local directory structure is as follows:
/webserver/mydomain.com/visa-information/etc...
On the live server it will be:
mydomain.com/visa-information/etc..
As i am using an Apache Alias on my local machine i have set RewriteBase as:
RewriteBase /webserver/mydomain.com/
I currently have the following set of RewriteRules adapted from what anubhava gave me:
RewriteCond %{REQUEST_URI} visa-information/country.php [NC]
RewriteCond %{QUERY_STRING} country=([A-Za-z-]+)&passport=([A-Za-z-]+) [NC]
RewriteRule visa-information/ visa-information/%1-visa-for-%2-citizens/? [R=301,L,QSA]
# internal redirect from pretty URL to old URL
RewriteRule ^visa-information/([A-Za-z-]+)-visa-for-([A-Za-z-]+)-citizens/?$ visa-information/country.php?country=$1&passport=$2 [NC,L]
This currently gives me an endless re-direct loop, both when entering the pretty url in the browser bar, and when using my form, however if i disable the top 3 rules then i find i can enter the pretty url into the address bar and the rewrite works, but not from the form submission of course.
I really don't know what i'm doing wrong. Why is there an endless loop?
回答1:
Not sure how first rule is working for you since RewriteRule
doesn't match leading slash in .htaccess
. Also to redirect old url to pretty URL you need to use THE_REQUEST
variable that represents original request received by Apache from your browser. Replace your code with this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from old URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+visa-information/country\.php\?country=([^\s&]+)&passport=([^\s&]+)\s [NC]
RewriteRule ^ /visa-information/%1-visa-for-%2-citizens/? [R=301,L]
# internal redirect from pretty URL to old URL
RewriteRule ^visa-information/([a-z-]+)-visa-for-([a-z-]+)-citizens/?$ /visa-information/country.php?country=$1&passport=$2 [NC,L,QSA]
回答2:
Looks like the issue was actually with my php form and not the mod_Rewrite rules. The form "action" was pointing to the same form page (as in $SERVER['PHP_SELF']) which works fine without the rewrite rules, but causes an endless loop when they are activated.
I simply made a search_action.php page and then redirect the form there using a php header to the pretty url:
header("Location: " . $dir . "/visa-information/" . $currentCountry . "-visa-for-" . $currentPassport . "-citizens/");
exit();
The mod_rewrite rule I had originally works fine and now the user can get to the desired page with the pretty url from the form or by tyoing directly into the browser address bar.
I'm not sure it's actually possible to action a form to the same page whilst rewriting the query string, without using an intermediary action page from the form, or Javascript. I'm sure many of the more experienced programmers will have known this already, but not me unfortunately.
来源:https://stackoverflow.com/questions/19142719/yet-another-mod-rewrite-query-cannot-rewrite-to-pretty-url-and-keep-query-str