当前位置: 动力学知识库 > 问答 > 编程问答 >

apache - Trailing slashes problem

问题描述:

When I type this "http://example.com/Hello%20There/" , it displays the

index page wich is : "http://example.com/Hello%20There/index.html" .

Well, what I want to do is when the user types "http://example.com/Hello%20There"

(so like the first one except it doesn't have a trailing slash).

I tried many things and specially regular expressions, but nothing works because I think

that the server stops the reg exp process when he finds a space ("%20" in the URL).

I tried this reg exp:

Options +FollowSymLinks

rewriteEngine On rewriteCond %{REQUEST_URI} ^(.*)\ (.*html)$

rewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]

rewriteCond %{ENV:space_replacer}!^$

rewriteCond %{ENV:space_replacer}!^.*\ .*$

rewriteRule ^.*$ %{ENV:space_replacer} [R=301,L]

and also put:

DirectorySlash On

in the "mod_dir" module of Apache.

So, my question is:

- How to tell to the server to add a trailing slash when the user types an url

without a trailing slash;$

网友答案:

You can make a character optional by appending the ? quantifier to it like this:

RewriteRule ^([^/]+)/?$ $1/index.html

Now both /foobar and /foobar/ would be rewritten to /foobar/index.html.

But it would be better if you use just one spelling, with or without the trailing slash, and redirect the other one:

# remove trailing slash
RewriteRule (.+)/$ /$1 [L,R=301]

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$1/ [L,R=301]

These rules either remove or add a missing trailing slash and do a permanent redirect.

网友答案:

I had the same problem, but I was using mod_alias to set up a subsite. Turns out, I needed to make a second alias without the trailing slash so that it would work correctly. Looked something like this:

Alias /forum/ "/var/www/forum"
Alias /forum "/var/www/forum"

<Directory "/var/www/forum">
    Options FollowSymlinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

In Ubuntu, I had to edit the /etc/apache2/mods-enabled/alias.conf file with these lines, then restart apache. Couldn't find this answer anywhere on the web; I just stumbled onto it myself as mod_rewrite wasn't working and the DirectorySlash command didn't help either. I was appending a non-Drupal program as a subsite under a Drupal installation, which is what kicked off all this madness in the first place...

网友答案:

Don't use trailing slash to define an alias.

Both URLs http://example.com/myalias1 and http://example.com/myalias1/ would work fine.

Example:

sudo vi /etc/apache2/apache2.conf            

Alias /myalias1 "/path/to/folder1"           
分享给朋友:
您可能感兴趣的文章:
随机阅读: