【问题标题】:URL redirect if found query string into url [closed]如果找到查询字符串,则 URL 重定向到 url [关闭]
【发布时间】:2020-08-11 20:12:13
【问题描述】:

当前的 URL 是 https://example.com/page/horeca,这是完美的。

但如果我输入“https://www.example.com/page/horeca”之类的“www”,页面将重定向到https://example.com/page.php?s=horeca

输入“https://www.example.com/page/horeca”后,我希望 URL 为“https://example.com/page/horeca”。即隐藏“www”并隐藏“.php”。

【问题讨论】:

  • 这需要一个 .htaccess 重定向。你的 RewriteCond 技能有多好?
  • RewriteEngine On RewriteBase / RewriteRule page/(.*) page.php?s=$1 [NC,L] RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https:// %{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [R=301 ,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]
  • 我也尝试过但不工作 重定向 301 ^/page.php?s=horeca example.com/page/horeca

标签: php .htaccess


【解决方案1】:

有两种方法可以做到这一点。在 php 页面中或在 .htaccess 文件中使用重写规则。以下链接将为您提供帮助 - Redirect Non-WWW to WWW URLs

【讨论】:

  • ## hide .php extension # 外部重定向 /dir/foo.php 到 /dir/foo RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s([^.]+ )\.php [NC] RewriteRule ^ %1 [R=302,L] # 内部转发 /dir/foo 到 /dir/foo.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*?)/?$ $1.php [L] 添加到@Mukund 答案,也许你可以看看这是否可以根据你的需要进行调整
【解决方案2】:

这应该是这样的:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteRule page/(.*) http://www.example.com/page.php?s=$1 [P]

应使用代理标志 [P] 加载第一个使用重定向 301 隐藏 www 的规则,然后是 https://www.example.com/page.php?s=horeca 以避免再次重定向。

注意:必须启用mod_proxy 才能使用此标志。

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2015-03-02
    • 2015-03-25
    • 2018-03-15
    • 2015-04-17
    相关资源
    最近更新 更多