【问题标题】:.htaccess ModRewrite: Url was not found.htaccess ModRewrite:找不到网址
【发布时间】:2021-08-16 02:52:13
【问题描述】:

我的目标是使用 .htaccess 将 http://ipaddress/directory/file.php?id=47 重定向到 http://ipaddress/directory/47id 参数会有所不同,可以是任何数字字符串,例如 1、2、3、10、20、300 等。我当前的 .htaccess 文件如下:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /directory/%1? [R=301,L]

重写确实将网址更改为我想要的!但是在被重定向后我得到一个错误,它指出,Not Found - The requested URL was not found on this server.我不确定为什么重定向有效,但没有加载上一个页面id 参数和实际的 PHP 文件。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    你需要内部重写

    RewriteEngine on
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
    RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
    
    RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
    

    RewriteCond %{ENV:REDIRECT_STATUS} ^$ 条件用于确保在从上一个规则完成重写后,重定向规则不会在mod_rewrite 的下一个循环中执行。

    【讨论】:

      【解决方案2】:

      对于您展示的示例,请尝试以下规则。确保您的 htaccess 存在于根文件夹中。确保在测试 URL 之前清除浏览器缓存。

      ##Making RewriteEngine ON here.
      RewriteEngine on
      ##Checking condition if THE_REQUEST is in same format which OP has mentioned for URI and mentioning Rule then.
      RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)\s [NC]
      RewriteRule ^ /directory/%1? [R=301,L]
      RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [NC,L,QSA]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-16
        • 2014-01-16
        相关资源
        最近更新 更多