【发布时间】:2016-05-28 06:01:25
【问题描述】:
我正在尝试使用以下代码引用网址(例如,从 page.php?page_id=1 到 page/1 或 page/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”,但没有成功
【问题讨论】:
-
您是否需要 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