mod_rewrite: add trailing slash?

前端 未结 4 960
心在旅途
心在旅途 2020-12-31 11:41

I am using a .htaccess file to direct requests for a directory to a custom php file that provides info about that directory (and i want the url displayed by the browser to n

相关标签:
4条回答
  • 2020-12-31 12:12

    Try this. It will add trailing slash to directory requests which don't have trailing slash.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    #use the directory viewer on directories without an index page
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*?)/?$ /myphp.php?url=$1/ [QSA,L]  
    
    </IfModule>
    
    0 讨论(0)
  • 2020-12-31 12:27

    Did you mean

    RewriteRule ^(.*)$ $1/ [L]
    

    instead?

    I don't know about your approach. I would try something like

    RewriteRule ^/(\S+)/?$ /myphp.php?url=$1
    

    I've used this for rewriting before and it works:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain\.com
    RewriteRule ^(.*)$ http://www.domain.com$1
    RewriteRule ^/(\S+)/?$ /myphp.php?url=$1
    

    Note that I didn't include the trailing slash in the first rule.

    Also, I would advice you not to rely on .htaccess unless absolutely necessary. This is because the server will check for .htaccess files constantly. Using the httpd.conf file instead means apache will only load conditions and rules once. At least I was adviced to do so because of this.

    0 讨论(0)
  • 2020-12-31 12:35

    Don't bother with Mod Rewrite, there is a directive for it

    <Location /some/path>
      DirectorySlash On
      SetHandler some-handler
    </Location>
    

    http://httpd.apache.org/docs/2.2/mod/mod_dir.html

    0 讨论(0)
  • 2020-12-31 12:36

    This line:

    RewriteCond %{REQUEST_FILENAME} -D
    

    Should have been:

    RewriteCond %{REQUEST_FILENAME} -d
    
    0 讨论(0)
提交回复
热议问题