【问题标题】:.htaccess Redirects wrong and looping.htaccess 重定向错误和循环
【发布时间】:2016-05-28 06:01:25
【问题描述】:

我正在尝试使用以下代码引用网址(例如,从 page.php?page_id=1page/1page/pagename 或只是 pagename):

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteBase /subdirectory/

# If user tries to access "subdirectory/page" without "get" then
RewriteRule ^page/ page.php [L]

# If user tries to access "subdirectory/page/1" or "subdirectory/pagename" then
RewriteRule ^page/1 page.php?page_id=1 [L]
RewriteRule ^page/pagename page.php?page_id=1 [L]

# If user tries to access "subdirectory/pagename" only then
RewriteRule ^pagename page.php?page_id=1 [L]

如果我关闭它(RewriteEngine Off 甚至删除整个 .htaccess 文件)并且如果我打开它,它会做错事 - 它将自己重定向到父目录(localhost www 文件夹),例如从subdirectory/page/etc.page/etc.。如果它最终起作用,在我一次又一次尝试(一次又一次......)以不停地更改它以使其工作后,当它有一个斜杠时会导致无限循环(例如page/1page/somepage)或者它只是无法正确显示页面(因为浏览器将其视为真实路径/文件夹而不是slug)

我知道我可能做错了什么(我是 .htaccess 的新手)但我不知道是什么……有什么想法吗?

非常感谢。

更新: .htaccess 文件位于“子目录”文件夹中。 www 文件夹(本地主机的主文件夹,也包括“子文件夹”)中还有另一个 .htaccess 文件,其中包含:

# RewriteEngine On
# 
# ErrorDocument 404 /err.php?id=404
# ErrorDocument 403 /err.php?id=403
# ErrorDocument 500 /err.php?id=500

评论只是因为这是禁用它的唯一方法-我尝试了“rewriteengine off”,但没有成功

【问题讨论】:

  • 您是否需要 RewriteBase,即该文件夹是否是物理目录的别名? .htaccess 放在哪里? “如果我关闭它......如果我打开它,它会做错事” - 关闭时它甚至不循环吗?
  • @Quasimodo'sclone 是的,因为我需要它在特定文件夹中工作。该文件夹是物理目录的别名。 .htaccess 文件放在“子目录”文件夹中
  • /subdirectory/somePageOrDirectory... 被请求但不存在时,您希望/subdirectory/page.php 被调用?
  • @Quasimodo'sclone 我认为这里存在误解。我可能没有很好地解释它。例如,如果“page_id”为 1,我想获取“somepageordirectory”。否则,将我重定向到 page.php。 (对不起,我的英语)
  • 为了澄清 - page.php 位于 /subdirectory/page.php?

标签: php apache .htaccess redirect mod-rewrite


【解决方案1】:

你可以使用这些规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /subdirectory/  
#if an existent file or dir is requested,serve it immideatly 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#####
    # If user tries to access "page" without "get" then
RewriteRule ^page/ page.php [L]

# If user tries to access "page/1" or "pagename" then
RewriteRule ^page/1 page.php?page_id=1 [L]
RewriteRule ^page/pagename page.php?page_id=1 [L]

# If user tries to access "pagename" only then
RewriteRule ^pagename page.php?page_id=1 [L]

【讨论】:

  • 我现在尝试添加RewriteRule ^ - [L] 并刷新,但仍然没有...还有其他想法吗?谢谢
  • 不仅是Rule,你还需要同时使用RewriteConds来防止rewrite循环。
  • 我上面已经有了RewriteCond %{REQUEST_FILENAME} -fRewriteCond %{REQUEST_FILENAME} -d
  • 否,但现在当我尝试访问 /subdirectory/pagename 并将其重定向到父文件夹(localhost/pagename 而不是 localhost/subdirectory/pagename)时会出现 404 错误
  • 回答有错误:规则默认是组合(是文件是目录),需要显式设置为 [OR]
【解决方案2】:

由于主要问题已经得到解答,这里是尾斜杠的附加问题。尝试添加重定向规则:

RewriteRule ^(.*)/$ /$1 [L,R=301]

这将回复 301 重定向 [R=301] 并停止处理进一步的规则。 [L]= 最后一条规则

例如/subdirectory/1/ 被重定向到 /subdirectory/1

现在由浏览器请求重定向的 URL。然后其他规则将生效。

【讨论】:

  • 在多次获取的情况下是否有效?我的意思是,如果我有例如page.php?page_id=1&something=2&something_again=3 它将工作并正确显示页面为pagename/something/something 等? (对不起,如果我写错了或不正确)
  • 它会向浏览器返回一个类似/subdirectory/1/的请求,告诉浏览器可以在/subdirectory/1找到该页面。然后客户端将完成一个新的重定向请求,其他规则开始执行。
猜你喜欢
  • 2013-02-08
  • 2014-10-08
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多