mod_rewrite将 site.com/something 定向到索引.php?值=某物


mod_rewrite to direct site.com/something to index.php?values=something

(我可能是个白痴,似乎在OSX上的Apache中默认不启用.htaccess。

我知道有很多mod_rewrite问题,但我就是不能正确回答我的问题,似乎没有其他东西可以完全涵盖它。

我只想重定向

www.url.com/something

www.url.com/something/anything/more

www.url.com/index.php?values=something

www.url.com/index.php?values=something/anything/more

没有什么比这更花哨的了(我会放一些关于忽略 gif/jpg/png,稍后添加 www. 等的东西) - 所有对某些东西/任何东西/更多内容的处理都将在 PHP 中完成。

不过我就是做不好。任何提示/资源或任何刚刚设法做到这一点的人?更令人沮丧的是,我曾经得到过绝对完美的代码,但几年前丢失了代码。

这很简单,至少在你所要求的方面是这样。 但基本上你需要做索引.php直通(所以你不会重写索引.php索引.php),然后是主要的重写。

RewriteEngine On
RewriteRule index.php - [L]
# your conditions here
RewriteRule ^/?(.*)$ /index.php?values=$1 [L]

# your conditions here点添加要排除重写的任何条件

RewriteEngineOn
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d    
RewriteRule ^(.*)$ /index.php?values=$1 [L]

请注意,如果您尝试在 .htaccess 文件中执行此操作,则需要在相关目录的 Apache 主机配置文件中设置允许覆盖打开,当然需要启用mod_rewrite模块。

此规则将忽略实际存在的任何实际文件或目录(即图像)的重定向(包括 index.php)。