Mod rewrite mod_重写友好的URL并删除页面扩展

Mod rewrite mod_重写友好的URL并删除页面扩展,mod-rewrite,url-rewriting,Mod Rewrite,Url Rewriting,我需要一点帮助,了解一些我正在尝试使用的mod_重写规则。我需要做两件事 更改动态URL以使其友好(例如/content.php?CategoryID=1更改为/categories/1/) 删除我的页面上的.php扩展名(例如/page2.php到/page2) 好的,我可以让数字1在我的.htaccess文件中单独使用以下内容: Options +Indexes Options +FollowSymlinks RewriteEngine on RewriteOptions Inherit

我需要一点帮助,了解一些我正在尝试使用的mod_重写规则。我需要做两件事

  • 更改动态URL以使其友好(例如
    /content.php?CategoryID=1
    更改为
    /categories/1/

  • 删除我的页面上的
    .php
    扩展名(例如
    /page2.php
    /page2

  • 好的,我可以让数字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
    
    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
    
    然后我尝试了第二个,我遇到了一些问题。我正在使用以下工具:

    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]
    
    上面的重新编写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
    
    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_重写和regex的东西都是新手。

    我知道这已经有一段时间了,我不是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]
    

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

    尝试使用mod_rewrite的日志记录(请参阅)。