mod_rewrite - 友好的URL和删除页面扩展名

时间:2010-09-03 20:47:06

标签: mod-rewrite url-rewriting

我需要一些我正在尝试使用的mod_rewrite规则的帮助。我需要做两件事

  1. 更改动态网址,使其友好(例如/content.php?CategoryID=1更改为/categories/1/

  2. 删除我网页上的.php扩展程序(例如/page2.php/page2

  3. 好的,所以我可以在我的.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]
    

    上面的内容会重新编写网址,使其看起来很友好(例如/page2/),但它会点击我的404页面而我无法查看,但我重写网址的其他页面仍然有效(例如{{1} })。

    这是我的完整.htaccess文件:

    /category/1/

    任何帮助都会非常感激,因为我是所有这些mod_rewrite和regex的新手。

1 个答案:

答案 0 :(得分:0)

我知道已经有一段时间了,我不是百分之百确定你要做什么,但我可能会看到:

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]

我需要更仔细地查看您的.htaccess和/或更好地了解您网站的结构,以便提供更好的信息。

相关问题