【问题标题】:How do I modify query strings in the URL to make them more user friendly如何修改 URL 中的查询字符串以使其更加用户友好
【发布时间】:2013-11-09 17:13:11
【问题描述】:

我的 .htaccess 文件中有以下内容,如果它不是目录,它会删除 .php 扩展名,并删除剩余部分之后的斜杠。但是,我想修改它以更改查询字符串。

AddType text/x-component .htc
RewriteBase /

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

我想将 {root}/file?id=1 更改为 /file/1 和 我想将 {root}/directory/file?id=1 更改为 {root}/directory/file/1

非常感谢任何帮助!

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    最后插入这 2 条新规则

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([0-9]+)/?$ /$1?id=$2 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/?$ /$1/$2?id=$2 [L,QSA]
    

    PS:还要确保在上面的所有RewriteRule 行中使用L 标志。

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多