mod_rewrite remove trailing slash not working in Laravel

独自空忆成欢 提交于 2019-12-08 13:22:56

问题


I have the following code setup in my htaccess file for my Laravel 3 installation:

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

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

Which looks all well and good untill the first ReWrite condition is accessed.

Navagating to this url:

/information/

Redirects me to:

/home/xxx/Dropbox/Development/laravel/httpdocs/information

So the trailing slash is being removed, but it redirecting to the abolsute URL.

Why is it doing that? How do I fix it?

Thanks :)


回答1:


Add a RewriteBase / (or whatever the base URI should be) above all of your rules.

What's happening is that mod_rewrite tries to guess if the target of a rewrite rule is a URL path or a file path and it is guessing incorrectly that it's a file path and appends the incorrect base path.



来源:https://stackoverflow.com/questions/14097941/mod-rewrite-remove-trailing-slash-not-working-in-laravel

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