【问题标题】:Url redirection issue using .htaccess and php使用 .htaccess 和 php 的 URL 重定向问题
【发布时间】:2016-04-10 06:16:25
【问题描述】:

我正在尝试使用 .htaccess 重写 url。我尝试了几种解决方案来访问所需的 url,但无法弄清楚。谁能帮忙。

原始网址

http://localhost/view_retailer.php?id=<storename>

使用 .htaccess 重写 url

http://localhost/view_retailer?id=<storename>

想要的网址

http://localhost/store/<storename>

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    你可以在你的 root .htaccess 中使用这两条规则:

    RewriteEngine On
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /view_retailer(?:\.php)?\?id=([^\s&]+) [NC]
    RewriteRule ^ /store/%1? [R=302,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^store/([^/]+)/?$ view_retailer.php?id=$1 [L,QSA,NC]
    

    【讨论】:

    • 谢谢阿努巴瓦。重定向似乎按预期工作,但我得到一个 404 页面。我尝试从 .htaccess 中删除所有内容,原始 url 工作正常。 http://localhost/view_retailer.php?id=<storename> 。一旦我将重定向放入 .htaccess 中,该 url 就可以像 http://localhost/store/<storename> 一样正常工作,但它会给我一个 404 错误页面。 chrome 中的网络选项卡正确显示它正在尝试重定向到 url http://localhost/view_retailer.php?id=<storename> 但收到 404 错误。
    • 同样的问题。没有运气
    • 完整的 .htaccess Options -Indexes ErrorDocument 404 /404.php ErrorDocument 403 / <FilesMatch "\.inc.php"> order deny,allow deny from all </FilesMatch> RewriteCond %{THE_REQUEST} /view_retailer(?:\.php)?\?id=([^\s&]+) [NC] RewriteRule ^ /store/%1? [R=302,L,NE] RewriteEngine On RewriteRule ^(admin)($|/) - [L] # internal forward from pretty URL to actual one RewriteRule ^store/([\w-]+)/?$ view_retailer.php?id=$1 [L,QSA,NC] 。这两个文件都位于 www 文件夹下。
    • .htaccess 现在看起来像 RewriteEngine On # internal forward from pretty URL to actual one RewriteRule ^store/([\w-]+)/?$ view_retailer.php?id=$1 [L,QSA,NC] url 已测试:localhost/store/flipkart.com 现在它说在此服务器上找不到请求的 URL /store/Flipkart.com。
    猜你喜欢
    • 2015-04-15
    • 2020-12-03
    • 1970-01-01
    • 2021-02-27
    • 2016-07-02
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    相关资源
    最近更新 更多