【问题标题】:.htaccess rewrite - filename.html to /?c=filename.htaccess 重写 - filename.html 到 /?c=filename
【发布时间】:2019-04-16 10:59:04
【问题描述】:

我需要它来处理所有 html URL(索引除外),我尝试使用 .htaccess 没有运气。

例如...将此链接从:http://www.example.com/signup.html 更改为 http://www.example.com/?c=signup

另外,如果我直接输入http://www.example.com/?c=signup,它必须镜像http://www.example.com/signup.html

编辑 我找到了这样做的方法,除了我直接输入.html文件的情况,在那种情况下,它不会重写。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^c=([^&]+) [NC]
RewriteRule ^/?$ %1.html [NC,L,QSA]

【问题讨论】:

    标签: apache .htaccess url mod-rewrite hyperlink


    【解决方案1】:
    RewriteRule ^/?$ %1.html [NC,L,QSA]
    

    重定向(大概你的意思是“重定向”,而不是“重写”)从signup.html/?c=signup,那么您可以在现有的重写之前执行以下操作:

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]
    

    检查REDIRECT_STATUS 环境变量的条件 可防止在现有指令重写到.html 文件时出现重定向循环。

    请注意,这是一个临时 (302) 重定向。将R 更改为R=301,如果这是永久的,但只有在您确认它工作正常后。

    总而言之,可以整理您现有的指令:

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    
    # Redirect from signup.html to /?c=signup
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]
    
    # Rewrite from /?c=signup to signup.html
    RewriteCond %{QUERY_STRING} ^c=([^&]+)
    RewriteRule ^/?$ %1.html [QSD,L]
    

    大概,你想从重写的替换中丢弃c=查询字符串?

    【讨论】:

    • 效果很好,非常感谢!我从“RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]” 中删除了“/”,如下所示:“RewriteRule ([^.]+)\.html$ ? c=$1 [R,L]" 因为我正在使用子文件夹。
    • 很高兴你能成功。但是,如果您只是删除 substitiution 上的斜杠前缀,那么除非您还将 RewriteBase 指令设置为该子目录,否则它将不起作用。
    • 我正在使用 RewriteBase /filehost/
    猜你喜欢
    • 2015-06-08
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多