简体   繁体   中英

Force trailing slashes in rewrite htaccess

My htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]

The results URL is /sentence-examples/acco-acti/accolading/

If I remove the last slash /sentence-examples/acco-acti/accolading still works fine

If I remove the third slash /sentence-examples/acco-acti <- 404's

Is there a way to force the slash so even if a user types the url without a slash it will change it to slash?

The problem with trailing slashes is they can modify the queries. For example:

 http://mysite.com/index.php?par=this_query
 // is differente from
 http://mysite.com/index.php?par=this_query/

In the last case, the $_GET value has a trailing slash, which usually leads to error.

So yes, there are ways to force trailing slashes in .htacces files, but it is not a good idea. It's already hard to figure out how to remove them in certain cases, why creating more.

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