Remove trailing slashes

后端 未结 4 1715
清歌不尽
清歌不尽 2021-01-19 00:11

I\'d like to work with pages without trailing slashes. So now I want my URL\'s with an trailing slash to redirect (using .htaccess) to the same URL without the trailing slas

相关标签:
4条回答
  • 2021-01-19 00:24

    Have you tried adding:

    RewriteBase /public
    

    to the .htaccess file in the public folder, to get:

    <IfModule mod_rewrite.c>
        RewriteEngine On            
        RewriteBase /public
    
        RewriteRule (.*)/$ $1 [R,L]
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f         
        RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
    </IfModule>
    
    0 讨论(0)
  • 2021-01-19 00:25

    Why do you have several .htaccess files? Where are the one's redirecting to public/$1 stored? You may very well be facing overriding RewriteRule directives that complicate things for you.

    Without knowing your setup it's fairly hard to say how you should be using rewrites - can you specify what things are ACTUALLY looking like?

    0 讨论(0)
  • 2021-01-19 00:29

    Here is a an example from Apache documentation which you can use to solve your issue:

    Trailing Slash Problem

    Description:

    Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.

    Solution:

    The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!

    So, to do this trick we write:

    RewriteEngine  on
    RewriteBase    /~quux/
    RewriteRule    ^foo$  foo/  [R]
    

    The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.

    RewriteEngine  on
    RewriteBase    /~quux/
    RewriteCond    %{REQUEST_FILENAME}  -d
    RewriteRule    ^(.+[^/])$ $1/ [R]
    

    Source

    0 讨论(0)
  • 2021-01-19 00:37

    Wordpress redirects depending on what your permalink structure is. So any changes to .htaccess helps little since Wordpress will add/remove them for you and peform a wp_redirect() after .htaccess done things. In worst case you end up with a redirect loop.

    A solution is to turn off Wordpress redirecting using.

    // perhaps an if(is_something()) before here ...
    add_filter('redirect_canonical', '__return_false');
    

    You should wrap this inside an if statement or such where you perform a check for a certain page/directory/etc. Putting the above directly in your functions file will turn off wordpress redirections and probably break things.

    0 讨论(0)
提交回复
热议问题