【问题标题】:Htaccess rewrite url in browser but dont affect the page (appearance url change only)Htaccess 在浏览器中重写 url 但不影响页面(仅外观 url 更改)
【发布时间】:2017-08-13 00:45:06
【问题描述】:

我在尝试在不更改其内容的情况下重写 url 时遇到问题。

我想在浏览器 url 中显示一个不同的 url,但内容还是一样的

我的网址是http://fortin.agency/audit-seo/frtcrwl/health_check/report/578/leasingautomobile.ro

我想把它改成http://fortin.agency/audit-seo/578/leasingautomobile.ro

我做了什么:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^frtcrwl/health_check/report/(.*)$ /$1 [L,NC,R=301,NE]

不起作用...有什么帮助吗?

编辑

url中的578是动态的,也是“leasingautomobile.ro”

编辑#2

最终代码:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(audit-seo)/frtcrwl/health_check/report/(.+)$ [NC]
RewriteRule ^ /%1/%2 [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L] 

仍然无法按预期工作,因为它会引发错误 404。路径可以保持不变吗?因为现在浏览器链接中的 url 是完美的。感谢@anubhava 直到现在

【问题讨论】:

  • 认为你需要更换你的 RewriteRule。首先是模式,然后是替换,例如像这样的东西(尽管不要引用我的话):RewriteRule ^/audit-seo/ /audit-seo/frtcrwl/health_check/report/(.*)$ /$1 [L,NC,R=301,NE]:addedbytes.com/articles/for-beginners/…
  • @justbaron 不,它不起作用。不改变 url,只改变路径,现在抛出 404
  • 手动输入时,女巫路径有效吗? http://fortin.agency/audit-seo/frtcrwl/health_check/report/578/leasingautomobile.rohttp://fortin.agency/audit-seo/578/leasingautomobile.ro?
  • 是的,但是没有任何重写规则,哪个有效?

标签: php apache .htaccess url mod-rewrite


【解决方案1】:

将您的规则更改为此并在 frtcrwl/.htaccess 内重新测试

Options +FollowSymlinks
RewriteEngine on
RewriteBase /audit-seo/frtcrwl/

RewriteCond %{THE_REQUEST} \s/+(audit-seo/frtcrwl)/health_check/report/(\S+) [NC]
RewriteRule ^ /%1/%2 [L,R=301,NE]

RewriteRule ^\d+/[^/]+/?$ index.php?/health_check/report/$0 [L,NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L,QSA]

【讨论】:

  • 非常感谢。你就像 htaccess 的神 :D 你的代码有效,但我发现使用 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php?/$0 [PT,L] 有一些奇怪的地方
  • 似乎它不起作用,因为这些选项会覆盖您的代码。是否可以修复它以执行我的期望并保留选项?
  • 好的,我把它放在我的代码上,它现在可以工作了,但它似乎不是一个有效的页面 (404)。有没有办法保留当前目录路径但显示不同的url?
  • 这样做了,清除了我的浏览器缓存并使用了您的代码,出现 404 错误,并且浏览器 url 中的 url 不再更改 :(
  • 好的,现在试试我更新的答案。这个 .htaccess 是否位于 audit-seo/ 目录中?
【解决方案2】:

如果您想能够从http://fortin.agency/audit-seo/578/leasingautomobile.ro 访问http://fortin.agency/audit-seo/frtcrwl/health_check/report/578/leasingautomobile.ro,您可以使用以下规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^audit-seo/(.+)$ audit-seo/frtcrwl/health_check/report/$1 [L,NC]

【讨论】:

  • 感谢您的回答,但您的代码不起作用...它不会更改网址
  • 你能看看我做的anubhava的cmets吗?他的代码确实改变了网址,但它也改变了路径。我不需要这样做,因为它会抛出 404
  • 不,它只是改变了路径,加载的路径但url保持不变
  • 我改变了答案。您现在可以从http://fortin.agency/audit-seo/578/leasingautomobile.ro 访问您的页面http://fortin.agency/audit-seo/frtcrwl/health_check/report/578/leasingautomobile.ro
  • 仍然 404 使用最新的源代码,链接:fortin.agency/audit-seo/578/leasingautomobile.ro
猜你喜欢
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 2015-02-18
  • 2017-06-30
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多