Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;$

like image 728
Zakaria Avatar asked Jan 05 '10 13:01

Zakaria


People also ask

Should I use trailing slashes?

Historically, a trailing slash marked a directory and a URL without a trailing slash at the end used to mean that the URL was a file. Today, however, trailing slashes are purely conventional, and Google does not care whether you use them; as long as you're consistent.

Does Google care about trailing slash?

Google treats each URL above separately (and equally) regardless of whether it's a file or a directory, or it contains a trailing slash or it doesn't contain a trailing slash.

Why do URLs have trailing slash?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash. However, these are guidelines and not requirements.

Should Canonical URL have trailing slash?

Google's John Mueller said on Twitter some time ago that the trailing slash after the domain name is an implied canonical. He said "The trailing slash after a domain name is always there, even if it's not shown." "It's always implied for canonicalization," he added.


2 Answers

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.

like image 183
Gumbo Avatar answered Sep 30 '22 14:09

Gumbo


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...

like image 20
John K Avatar answered Sep 30 '22 15:09

John K