简体   繁体   中英

.htaccess redirect domain.com to domain.com/

Is it possible to use a .htaccess file to redirect mydomain.com to mydomain.com/

I tried:

RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1

which should work for any URL on the domain however it only works for non-root URLs (Above code removes trailing slash rather than adds it, however I wanted to see if it'd work for root URLs).

There was also the solution from here which is:

RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ (.*)//([^\ ]*)
RewriteRule ^ %2/%3 [R=301,L]

This removes multiple trailing slashes (ie mydomain.com// becomes mydomain.com/) but still doesn't work for mydomain.com -> mydomain.com/

I'd even accept a way to do it with PHP if that's available.

http://mydomain.com and http://mydomain.com/ look exactly the same to the webserver, the URI is / for both of them. You're not going to be able to force the browser's address bar to have a trailing slash after the domain from .htaccess or php.

Try:

RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM