首页 文章

带有参数的url的.htaccess重写规则

提问于
浏览
1

另一个url重写问题,道歉,但有一点麻烦 .

我从文件中删除了 .php 扩展名:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php

注意:[ app/ 设置为公共目录]

注意:[ Apache 服务器]

Goal

我想要的是像这样的 URLs

www.example.com/app/user/123 而不是 www.example.com/app/user.php?id=123

我试过了: RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [R=301,L,NC] 但它只返回 500 Internal Server Error

  • 这也会影响 GETPOST 请求,还是需要对_1039490进行编码以反映网址更改?

Links I came across

right order of rewrite rules in an htaccess file

http://corz.org/serv/tricks/htaccess2.php

http://www.workingwith.me.uk/articles/scripting/mod_rewrite

.htaccess RewriteRule to preserve GET URL parameters

加上许多其他人 .

EDIT

删除 .php 的第一条规则是否会影响之后的规则?即阻止他们处理

1 回答

  • 1

    规则的排序可能会导致这个问题 . 按正确顺序尝试这些规则:

    RewriteEngine On
    RewriteBase /app/
    
    RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [QSA,L,NC] 
    
    # hide .php extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/app/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]
    

相关问题