这是我的.htaccess
代码
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^hashstar.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.hashstar.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php timer/index.php
我的根目录设置为文件夹app
在app文件夹中,我有2个文件夹
1.) Manage
2.) Timer
要访问管理文件夹,我必须输入网址
http://www.example.com/manage/
要访问计时器文件夹,我必须输入网址
http://www.example.com/timer/
如果我通过在网址(/)
中添加斜杠来使用这些网址
网址加载完美
但是如果不在URL中添加(/)
,那么它会通过根目录进行重定向
网址应该看起来像=> http://www.example.com/manage
变得像=> http://www.example.com/app/manage/
背后的原因是什么?如何解决这个问题?
任何帮助表示赞赏。提前谢谢。
答案 0 :(得分:1)
一目了然(我没有尝试在我的Apache2服务器上重现这些规则),这似乎是罪魁祸首:
RewriteRule (.*) /app/$1 [L]
正则表达式(.*)
将捕获Any character greedily
。您的重写规则只是在域和脚本之间添加/app/
。
Find and group <all non-null characters>,
and transform it to /app/<all non-null characters>
网址www.example.com/myscript.php
将被重写为www.example.com/app/myscript.php
修改强>
那么您建议使用哪些代码编辑? @SsJVasto
您可以简单地从贪婪规则中排除/
:
RewriteRule (.*)/ $1 [L]
这样,您将anything ending with a /, but without said /
的结果存储到$1
中。由于/不在组内,它将被遗忘。
如果您想处理重复(例如http://www.example.com/myscript.php//
),则可以添加+
符号来处理one or more occurrences
:
RewriteRule (.*)/+ $1 [L]
答案 1 :(得分:1)
此重定向的原因是重写的URI是一个没有尾部斜杠的真实目录。发生这种情况时,Apache的mod_dir模块通过重定向到带有斜杠的URI来对其进行操作。
要防止使用此.htaccess:
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?hashstar.com$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]