【发布时间】:2011-03-03 06:46:24
【问题描述】:
我很难使用 .htaccess。我想为我正在开发的网站创建友好的 URL...
基本上我想转换这个:
http://website.com/index.php?ctrl=pelicula&id=0221889
http://website.com/index.php?ctrl=pelicula&id=0160399&tab=posters
进入这个:
http://website.com/pelicula/0221889/
http://website.com/pelicula/0221889/posters/
如果我以后需要它,我也想知道如何像这样将文章标题添加到 URL 的末尾(我使用的是 PHP):
http://website.com/pelicula/0221889/the-article-name/
http://website.com/pelicula/0221889/the-article-name/posters/
注意:Stackoverflow的方法对我也有好处,比如这个问题的url是:
http://stackoverflow.com/questions/3033407/htacces-to-create-friendly-urls-help-needed
但是你可以在 id 后面放任何东西,它也可以工作。像这样:
http://stackoverflow.com/questions/3033407/just-anything-i-want
我使用了一些自动网络工具来创建 .htacces 文件,但它无法正常工作。所以我请求你的帮助。
如果您能推荐 .htacces 最佳实践和建议,我也会很高兴..
编辑:根据我得到的一些答案,我把这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]+)/([^/]+)/?([^/]*)/?$ index.php?ctrl=$1&id=$2&tab=$3 [QSA,L]
</IfModule>
但我收到默认主机“找不到页面”错误。
我也试过了:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(\d+)/([^/]+)/?$ index.php?ctrl=$1&id=$2&tab=$3 [QSA,L]
RewriteRule ^([^/]+)/(\d+)/?$ index.php?ctrl=$1&id=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ index.php?ctrl=$1 [QSA,L]
</IfModule>
这也行不通。它会将我带到我的默认 404.php 页面。
mod_rewrite 已启用并正在工作。
救命!
【问题讨论】:
-
要查看您的 URL 被重写为什么,请尝试在 index.php 的顶部回显 $_GET 或使用重写日志进行调试。 httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog
-
你“真的”确定重写工作正常吗?我曾经在配置错误的 apache 配置文件时遇到过同样的问题。 webune.com/forums/…真的帮了我
标签: php url .htaccess url-rewriting