【问题标题】:How to keep original URL in addressbar after RewriteRuleRewriteRule 后如何在地址栏中保留原始 URL
【发布时间】:2015-11-13 21:04:44
【问题描述】:

这是我的 .htaccess 文件。

Options +FollowSymLinks

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

RewriteEngine on

RewriteRule ^index.html             /? [R=301,L]
RewriteRule ^listen/$               /console [R=301,L]


# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

重写规则

RewriteRule ^listen/$               /console [R=301,L]

将网址www.mydomain.co.uk/listen重定向到www.mydomain.co.uk/console

此重定向工作正常,但我希望地址栏中的可见 url 能够读取...

www.mydomain.co.uk/listen

非常感谢任何帮助。谢谢

【问题讨论】:

  • 删除 R=301 标志以使其成为 RewriteRule ^listen/$ /console [L]
  • 谢谢。我试过了,但还是不行……
  • 清除浏览器缓存。
  • 是的,我做了几次,并在不同的浏览器中尝试过
  • 是不是因为后面出现了其他重写规则( RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L] )?我尝试更改顺序,但重定向完全中断

标签: .htaccess url mod-rewrite url-redirection http-redirect


【解决方案1】:

这样吧:

Options +FollowSymLinks
RewriteEngine on

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^index\.html$ /? [R=301,L,NC]

RewriteRule ^listen/$ /console/ [NC,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

清除浏览器缓存后进行测试。

【讨论】:

  • 谢谢。感谢你的帮助。那行得通。但是,我(天真地)没想到会发生一件事,就是所有与 css / js 等相关的链接现在都被破坏了!
  • 那是由于使用了相对链接。您可以将其添加到页面 HTML 的 &lt;head&gt; 部分:&lt;base href="/" /&gt;
猜你喜欢
  • 2021-05-16
  • 1970-01-01
  • 2012-03-17
  • 2017-05-09
  • 2012-05-22
  • 2010-10-20
  • 2017-10-26
  • 2010-09-21
  • 2021-06-18
相关资源
最近更新 更多