开发者

Forcing a trailing slash to a URL after a subdomain rewrite

开发者 https://www.devze.com 2023-01-14 18:39 出处:网络
I\'m re-writing a subdomain to a \'folder\' - actually a page in wordpress, and this all seems to be working correctly. I don\'t want the address in the URL bar to change though.

I'm re-writing a subdomain to a 'folder' - actually a page in wordpress, and this all seems to be working correctly. I don't want the address in the URL bar to change though.

Everything works fine unless the user does not pu开发者_Go百科t a trailing slash after the page name, then the page is still redirected to the correct URL but the URL in the address bar changes

For example: foo.example.com/bar Becomes: public.example.com/foo/bar

Where : foo.example.com/bar/ stays at the correct URL in the address bar but shows the redirected page (this is correct)

What rule do i need to add to add in a trailing slash if its not sent?

The code i have so far is:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^(.*)$ http://public.example.com/foo/$1 [P]

# rules for WordPress ...

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

#####

</IfModule>

Any help would be fantastic, I'm pretty new to htaccess. Thanks!


Phew, after a bit of playing around i seem to have got it working:

RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://foo.example.com/$1/ [L,R=301]

RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^(.*)$ http://public.example.com/foo/$1 [P]

Basically, the first block adds a trailing slash to the URL is it's not there in the first place, then the second block does the proxy redirect for the URL.

As far as i can see this catches all cases, but let me know if there are any gaping holes!


This rule should do it:

RewriteRule .*[^/]$ %{REQUEST_URI}/

Put this rule in front of your other rules. You also may want to add a condition to exclude existing files:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/
0

精彩评论

暂无评论...
验证码 换一张
取 消