【问题标题】:URL Trailing Slashes Effecting The HTACCESS Redirection影响 HTACCESS 重定向的 URL 尾部斜杠
【发布时间】:2013-10-29 21:30:47
【问题描述】:

我已使用以下 Htaccess 成功地将我的网站 URL 从 "localhost/example/user.php?u=user1" 重定向到 "localhost/example/user1"代码:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ user.php?u=$1

但是现在,当我打开我的网站时,我的网站带有一个尾随斜杠 "localhost/example/user1/",它会将我重定向到 localhost/user1,我尝试了很多方法来消除 url 中的斜杠,但它不起作用。

如何实现?

谢谢。

【问题讨论】:

  • 你在某个地方还有其他的模组重写规则吗?
  • @JonLin,不,先生,这是我的 .htaccess 文件的完整编码,#1 1--- Establish a custom 404 page. ErrorDocument 404 /filenotfound.php #2 2----- Prevent directory file IndexIgnore * RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/\.]+)/?$ user.php?u=$1

标签: php .htaccess redirect


【解决方案1】:

将 RewriteBase 设置为您的工作目录。

RewriteBase /example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ user.php?u=$1

【讨论】:

  • RewriteBase is not working 它给出这个错误:Not Found The requested URL /var/www/web_intersect/user.php was not found on this server.
  • 在尝试了很多之后,我已将 RewriteBase 设置为 RewriteBase /web_intersect/ 并且当我尝试在末尾使用斜杠打开站点时 localhost/example/user1/ 它打开但没有 CSS 样式。
  • 对不起我的错。我编辑了上面的代码。你如何包含你的 CSS 样式?您应该提供绝对路径。因此,如果您像 <link rel="stylesheet" type="text/css" href="style.css" /> 这样包含它们,则必须提供像 <link rel="stylesheet" type="text/css" href="/class.css" /> 这样的绝对路径。或者,您可以像这样设置基本网址<base href="http://localhost/example" />。
  • 我包含了 <link rel="stylesheet" type="text/css" href="style.css" /> 之类的样式表,我将它们更新为 <link rel="stylesheet" type="text/css" href="localhost/css/style.css" /> 并且它起作用了。谢谢。
猜你喜欢
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 2021-05-21
  • 1970-01-01
相关资源
最近更新 更多