【问题标题】:htaccess Dynamic Redirect that changes URL in clients browser在客户端浏览器中更改 URL 的 htaccess 动态重定向
【发布时间】:2021-04-01 13:12:09
【问题描述】:

我正在尝试将 /search.php?s=xxx 重定向到 /search/xxx 并在客户端的地址栏中实际显示 /search/xxx。

我在 htaccess 根目录中使用以下指令,它可以工作但不会更改 URL

RewriteRule           ^search/(.*)$           /search.php?s=$1

我尝试在语句末尾添加 [R=301],所以:

RewriteRule           ^search/(.*)$           /search.php?s=$1 [R=301]

但这恰恰相反,这意味着它将 /search/xxx 更改为 /search.php?s=xxx

这是整个 .htaccess 文件:

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule           ^monumentum$                  /new_monumentum.php
RewriteRule           ^monumentum/(.*)$             /new_monumentum.php?mid=$1

RewriteRule           ^nuntium$                     /new_articulo.php
RewriteRule           ^nuntium/(.*)$                  /new_articulo.php?aid=$1

RewriteRule           ^liber$                         /new_liber.php
RewriteRule           ^liber/(.*)$                  /new_liber.php?lid=$1

RewriteRule           ^introductio$               /new_pagina.php
RewriteRule           ^introductio/(.*)$            /new_pagina.php?pid=$1

RewriteRule           ^persona$               /new_profile.php
RewriteRule           ^persona/(.*)$          /new_profile.php?cid=$1

#RewriteRule          ^search$                /new_search.php
#RewriteRule          ^search/(.*)$           /new_search.php?sea=$1

RewriteRule           ^exitio$                /new_exit.php
RewriteRule           ^intro$                 /new_enter.php
RewriteRule           ^novus$                 /new_account.php

# Error 404: Paginas no encontradas
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

ErrorDocument 403 /403.html
ErrorDocument 404 /404.html

【问题讨论】:

    标签: php apache .htaccess redirect


    【解决方案1】:

    要将/search.php?s=xxx 重定向到/search/xxx,您可以使用以下规则:

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} /search\.php\?s=([^\s&]+) [NC]
    RewriteRule ^ /search/%1? [R=301,L,NE]
    
    RewriteRule ^search/(.+)$ search.php?s=$1 [L,NC,QSA]
    
    # place your remaining rules below here
    
    RewriteRule           ^monumentum$ new_monumentum.php [L]
    RewriteRule           ^monumentum/(.*)$ new_monumentum.php?mid=$1 [L,QSA]
    
    RewriteRule           ^nuntium$ new_articulo.php [L]
    RewriteRule           ^nuntium/(.*)$ new_articulo.php?aid=$1 [L,QSA]
    
    RewriteRule           ^liber$ new_liber.php [L]
    RewriteRule           ^liber/(.*)$ new_liber.php?lid=$1 [L,QSA]
    
    RewriteRule           ^introductio$ new_pagina.php [L]
    RewriteRule           ^introductio/(.*)$ new_pagina.php?pid=$1 [L,QSA]
    
    RewriteRule           ^persona$ new_profile.php [L]
    RewriteRule           ^persona/(.*)$ new_profile.php?cid=$1 [L,QSA]
    
    RewriteRule           ^exitio$ new_exit.php [L]
    RewriteRule           ^intro$ new_enter.php [L]
    RewriteRule           ^novus$ new_account.php [L]
    
    # Error 404: Paginas no encontradas
    ErrorDocument 403 /403.html
    ErrorDocument 404 /404.html
    

    【讨论】:

    • 感谢您的宝贵时间,但仍然无法正常工作。与我之前的答案相同的行为。
    • 我对这个答案的最后评论你之前写的一切都很好,除了/search/xxx。在我的最新更新中,这个问题也得到了解决。
    猜你喜欢
    • 1970-01-01
    • 2012-02-14
    • 2017-03-30
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多