relative path doesn't work after adding a rewrite rule for appending trailing slash

时间秒杀一切 提交于 2020-01-25 00:07:22

问题


I have an index page placed in /forum/section1/. I have a rule to automatically redirect to the index page when I browse exampledomain.com/forum/section1/. Sometimes when I forgot the trailing slash, it keeps loading and never goes to the index page. I have added a rule to append a trailing slash to every request:

RewriteEngine on
#From http://enarion.net/web/htaccess/trailing-slash/ 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
# Don't apply to images/css/js/ajax files
RewriteCond %{REQUEST_URI} !^.*\.(jpg|jpe?g|ico|css|js|gif|png|php)$ [NC]
RewriteRule ^(.*)$ http://exampledomain.com/$1/ [L,R=301] 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^.*\.(jpg|jpe?g|ico|css|js|gif|png)$ [NC]

#Remove php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

The rules are working, but all of the relative paths become invalid. For example, I have a form on the index page:

       `<form action='index' method='POST'></form>`

Instead of accessing exampledomain/forum/section1/index/, it would redirect to exampledomain/forum/section1/index/index. I know changing relative paths to absolute paths would fix the problem, but is there a way to fix that with the rewrite rules?

来源:https://stackoverflow.com/questions/30586842/relative-path-doesnt-work-after-adding-a-rewrite-rule-for-appending-trailing-sl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!