问题
I am using url rewriting to have artists/(artistname) go to artists/index.php?(artistname).
I use this code to do the url rewriting
RewriteRule ^artists/(.+)$ artists/index.php?$1
I then use this code to get the part of the URL after the ? and save it as a variable
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$artist = parse_url($pageURL, PHP_URL_QUERY);
That code works when I don't use the URL rewriting, but not when I use it. I have checked it by echoing $artist. Does anyone know what might be going wrong?
回答1:
Your rewrite rule needs to be fixed. It looks like your rule could cause a redirect loop, this should result in a server 500 error, surprised that it doesn't. Try something more like this.
RewriteCond %{REQUEST_URI} !^/artists/index.php.*
RewriteRule ^artists/(.+)$ artists/index.php?$1 [L]
The conditional statement should eliminate a the rule from looping. Additionally add the [L] flag to the rule to stop the rule from processing more at that point.
回答2:
Because your new Rewrite rule, gets rid off the params your parse_url
for the Url querys wont work. You can try instead:
$artist = basename(parse_url($pageURL, PHP_URL_PATH));
回答3:
I ended up using a different way to get the URL that worked
$artist = $_SERVER["REQUEST_URI"];
$artist = substr($artist, 9, -1);
来源:https://stackoverflow.com/questions/19193444/url-rewriting-and-save-end-of-url-as-variable