Htaccess 用 id 重写

Htaccess rewrite with id

我在将 edit.php?id=1 重写为 edit/id/1 时遇到了问题。现在我的 .htaccess 文件中有以下内容:

RewriteEngine On
RewriteBase /

RewriteRule ^edit/id/([^/.]+)$ edit.php?id= [NC]

这不会改变 url。有人能看出我做错了什么吗?

试一试,看看效果如何。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/edit/id/([^/.]+)$ /edit.php?id= [NC,L]
   </IfModule>

或者如果您只有几个相同的目录,您也可以这样做。将进入根 .htaccess 文件。

  <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(profiles|customers|test)/edit/id/([^/.]+)$ /edit.php?id= [NC,L]
       </IfModule>

您需要一个额外的规则来从外部更改 URL。 这应该放在根.htaccess:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]

RewriteRule ^([^/]+)/edit/id/([^/.]+)$ /edit.php?id= [NC,L,QSA]