URL Rewriting and save end of URL as variable

爷,独闯天下 提交于 2019-12-11 05:47:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!