在最后一个斜杠后忽略URL中的最后一个字符串

时间:2012-06-01 02:39:56

标签: php apache .htaccess url-rewriting

例如:当用户输入www.website.com/site/blue时,我希望他们在网络浏览器中转到www.website.com/site/index.php。这样做的原因是我想使用php从URL抓取蓝色并将其放在www.website.com/site/index.php

因此当有人输入www.website.com/site/blue时......他们应该在"Hello blue!!!"

中看到www.website.com/site/index.php

3 个答案:

答案 0 :(得分:2)

听起来你需要使用htaccess文件和Apache的mod_rewrite。如果允许在服务器上执行此操作,请在.htaccess目录中创建名为site的文件。

RewriteEngine on
RewriteRule ^(.*)$ index.php?color=$1

这会将每个请求重写为index.php的查询字符串,供您解析。

答案 1 :(得分:0)

基本上,您可以将页面URL拆分为“/”,它将为您提供URL的一部分数组。在您的示例中,这些部分中的最后一个将是“蓝色”。尝试以下内容:

var parts = document.URL.split("/");
var foo = parts.pop();
var url = parts.join("/") + "/index.php";
window.location.href = url;

然后变量foo是您正在寻找的变量“蓝色”。

答案 2 :(得分:0)

试试这个htaccess配置:

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
相关问题