Force trailing slash at end of rewritten query str

2019-08-17 14:57发布

Here's what my htaccess looks like:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule .? http://site.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_URI} /page1/([^?].*)$ [NC]
RewriteRule /page1/(.*)$ /page1/?$1 [L] # change /page1/stuff to /page1/?stuff
# End rewriting for page1


RewriteCond %{REQUEST_URI} /page2/([^?].*)$ [NC]
RewriteRule /page2/(.*)$ /page2/?$1
# End rewriting for page2



RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,NS,L]

Options -Indexes

What I also want is to have it that if a user goes to site.com/page1/stuff or site.com/page1/stuff/more it will add a trailing slash in the address bar and change it to site.com/page1/stuff/ and site.com/page1/stuff/more/ but nothing I tried works. Is there any way to do this without causing a redirect loop?

I want all my urls to have a trailing slash and I see directories already doing that.

EDIT: To clarify, I want the trailing slash to be visible to the user but not the ?

标签: php .htaccess
1条回答
女痞
2楼-- · 2019-08-17 15:41

Thank for that link @LazyOne, anyway this works:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
查看更多
登录 后发表回答