2010-09-03 50 views
1

我需要一些mod_rewrite规则,我尝试使用一些帮助。我需要做两件事情mod_rewrite - 友好的URL和删除页面扩展

  1. 变化的动态URL,这样它的友好(例如,/content.php?CategoryID=1变化/categories/1/

  2. 在我的网页中删除.php扩展(如/page2.php/page2

好吧,我可以在我的.htaccess文件中使用以下代码自行工作:

Options +Indexes 
Options +FollowSymlinks 

RewriteEngine on 
RewriteOptions Inherit 
RewriteBase/

RewriteRule ^category/([0-9]+)?$ category/$1/ [R] 
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1 

我然后尝试2号,我遇到了几个问题。我使用了以下内容:

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC] 
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L] 

RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{SCRIPT_FILENAME} -f 
RewriteCond %{REQUEST_URI} ^(/.+)\.php$ 
RewriteRule ^(.+)\.php$ %1/ [R=301,L] 

RewriteCond %{REQUEST_URI} ^(/.+)/$ 
RewriteCond %{DOCUMENT_ROOT}%1.php -f 
RewriteRule ^.+/$ %1.php [QSA,L] 

以上rerwrites的URL,所以它看起来友好(如/page2/),但它击中我的404页,在这里我重写URL仍然有效,我无法查看,但我的其他网页(例如/category/1/)。

这是我的全部.htaccess文件:

Options +Indexes 
Options +FollowSymlinks 

RewriteEngine on 
RewriteOptions Inherit 
RewriteBase/

RewriteRule ^category/([0-9]+)?$ category/$1/ [R] 
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1 

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC] 
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L] 

RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{SCRIPT_FILENAME} -f 
RewriteCond %{REQUEST_URI} ^(/.+)\.php$ 
RewriteRule ^(.+)\.php$ %1/ [R=301,L] 

RewriteCond %{REQUEST_URI} ^(/.+)/$ 
RewriteCond %{DOCUMENT_ROOT}%1.php -f 
RewriteRule ^.+/$ %1.php [QSA,L] 

ErrorDocument 404 /misc/404page.html 

任何帮助,因为我是新来的这一切的mod_rewrite和正则表达式的东西将不胜感激。

+0

尝试使用mod_rewrite的日志记录(请参阅['RewriteLogLevel'](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel))。 – Gumbo 2010-09-04 12:15:23

回答

0

我知道它已经有一段时间,我不是100%肯定是你所试图做的,但我可能会被看:

RewriteRule ^category/([0-9]+)$ category/$1/ [R,L] 
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L] 
RewriteRule \.php? - [L] 
RewriteRule ^(.+)\.php$ $1 [R,L] 
RewriteRule ^([^/?])$ $1.php [L] 

我需要看看你的。更好地访问和/或更好地了解您的网站的结构以提供更好的信息。