繁体   English   中英

.htaccess 重定向错误和循环

[英].htaccess Redirects wrong and looping

我正在尝试使用以下代码引用网址(例如从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/1时导致无限循环或page/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”,但没有成功

您可以使用这些规则:

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 ^(.*)/$ /$1 [L,R=301]

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM