【问题标题】:How to use 2 Passed Parameters to Change URL With htaccess?如何使用 2 个传递的参数通过 htaccess 更改 URL?
【发布时间】:2014-03-15 16:48:19
【问题描述】:

如果我有一个看起来像这样的网址:

localhost/category.php?id=name&phrase=something

我可以使用什么 .htaccess 重写规则代码使其看起来像:

localhost/name/something 

以下是我的 .htaccess 文件目前的样子:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]

# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

大家有什么想法吗?

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    你可以使用这条规则:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    # external redirect from /category.php?id=name&phrase=something to /name/something
    RewriteCond %{THE_REQUEST} \s/+category\.php\?id=([^&]+)&phrase=([^\s&]+) [NC]
    RewriteRule ^ %1/%2? [R=302,L]
    
    # skip files and directories from rewrites
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^/?article/([^/]+)/?$ article.php?id=$1 [L,QSA]
    
    RewriteRule ^/?wrestler/([^/]+)/?$ wrestler.php?id=$1 [L,QSA]
    
    # Removes the .php extension from pages
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    RewriteRule ^([^/]+)/([^/]+)/?$ category.php?id=$1&phrase=$2 [L,QSA]
    

    【讨论】:

    • 我试图做的是匹配 /name/keyword 然后(内部)重定向到我的 /category.php?id=name&keyword=something
    • 当我尝试输入 localhost/name/something 之类的内容时,出现 404 Not Found 错误。
    • 您是否在 .htaccess 中添加了最新规则?
    • 它工作的兄弟!非常感谢。您是否推荐任何有关此 .htaccess 内容的阅读资源?这真是令人难以置信的混乱。
    • 很高兴知道它有效。对我来说,最好的参考是askapache.com 网站
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多