【问题标题】:htaccess - remove .php extension and keep variableshtaccess - 删除 .php 扩展名并保留变量
【发布时间】:2021-07-11 05:44:58
【问题描述】:

原始网址 - example.com/stars.php?id=9&s=lorem-ipsum
想成为-example.com/stars/9/lorem-ipsum

这是我的尝试 - 收到错误 500

RewriteCond %{THE_REQUEST} \s/stars\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)\.php$ /stars.php?id=$1&s=$2 [L]

.htaccess 和 stars.php 放在根目录-example.com

请帮忙

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting friendly-url


    【解决方案1】:

    对于您显示的示例,您能否尝试以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。

    RewriteEngine ON
    ##Rule to handle external redirection to user friendly URL here.
    RewriteCond %{THE_REQUEST} \s/(stars)\.php\?id=(\d+)&s=([^\s]*) [NC]
    RewriteRule ^ %1/%2/%3? [R=301,L]
    
    ##Rule to handle non-existing files/directories should be served by php files in backend.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1.php?id=$2&s=$3 [L]
    

    【讨论】:

    • 我清除缓存并尝试。没有错误,但也没有漂亮的网址。地址栏是 -example.com/stars.php?id=9&s=lorem-ipsum。另外,我只想在当前页面是 stars.php 而不是 planets.php 时重写 - 例如。
    • @vegas,好的,你能试试我编辑的规则一次吗?
    • 现在我知道了 - example.com/stars/9/lorem-ipsum?id=9&s=lorem-ipsum
    • @vegas,好的,有 1 个错字,我现在已经修正了,请检查一次,然后告诉我?欢呼
    • 它有效。非常感谢您的所有努力。我会为 htaccess 的一切而疯狂
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2011-04-30
    相关资源
    最近更新 更多