Two attempts at url rewriting, not working

I use the following to rewrite urls to not have a trailing slash:

RewriteRule ^(.*)/$ /$1 [L,R=301]

But for the home page, localhost.com/, it will not remove multiple trailing slashes. So localhost.com/// will stay localhost.com/// and not be rewritten. What am I doing wrong here?

I also use this rule to rewrite to mysite.com if I go to mysite.com/index.php, but again, it doesn’t seem to be working.

RewriteRule ^(index\\.php)$ mysite.com [QSA,L]

Any thoughts? I am new to mod rewrite and most of these are altered examples I found around by googling previous questions, but I couldn’t fix these.

But for the home page, localhost.com/, it will not remove multiple trailing slashes. So localhost.com/// will stay localhost.com/// and not be rewritten. What am I doing wrong here?

Ahh.

So, rewrite rules actually operate on directory paths, so it seems that when converting the URL path to a directory path, Apache collapses the multiple slashes, which is why the rewrite rule can’t detect them.

You’ll have have to switch to checking %{THE_REQUEST} in a rewrite condition. THE_REQUEST is the full HTTP request line sent by the browser to the server (e.g., “GET /index.html HTTP/1.1”).

RewriteCond %{THE_REQUEST} " /(.*?)/+ "
RewriteRule ^ /%1 [R=301,L]

Some notes about the above.

  • The quotes and the spaces in the condition are meant to be there. The quotes make the spaces be part of the matching pattern, and the spaces are part of the request line and delimit the URI.
  • The request URI will always include the initial slash of the path, so that’s meant to be there too.
  • The rewrite rule pattern is written so that it will always match anything. That’s because all the real work is being done in the condition.

Be back in a bit for the second half of your question.

Thanks for the help so far. I will try this later today and report the results.

(sorry a little late!)

So here is what happened.

I tried your suggestion:

RewriteCond %{THE_REQUEST} " /(.*?)/+ "
RewriteRule ^ /%1 [R=301,L]

But none of my pages worked - they weren’t found by apache. Perhaps it was because page request are no longer routed back to index.php?

It would probably be helpful for you to have my full .htaccess file, so here it is:


RewriteEngine On

RewriteRule ^(.*)/$ /$1 [L,R=301]  # Remove trailing slashes

RewriteBase /test-site/one/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php [QSA,L]


Is there a specific place I should insert your code?

Seems like it should work if you put it in place of your current “Remove trailing slashes” line.

[FONT=Courier New]RewriteEngine On

RewriteCond %{THE_REQUEST} " /(.*?)/+ "
RewriteRule ^ /%1 [R=301,L]

RewriteBase /test-site/one/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php [QSA,L][/FONT]

Thanks for working with me on this. I am still encountering errors.

I tried what you posted and firefox told me that the request is being redirected in a way that will never complete. I’m at a loss here; my htacess file is exactly as you posted. Maybe the regex is re-capturing the request each time? I am attempting to go to http://localhost/test-site/one/ .

Ahh. Locked in an epic battle with Apache’s mod_dir. When the path maps to a real directory, then mod_dir will send a redirect with a trailing slash. But then whenever we get a request with a trailing slash, we send a redirect with the slash removed. Then mod_dir adds it. Then we remove it. So on and so on.

Which of the two would you prefer win? We could disable the mod_dir module, or we could restrict slash-removal to only non-real paths.

If I understand you correctly, is this a choice between having “example.com” and “example.com/” (with the / slash)? If so, I’d like non real paths like “example.com///” to redirect to “example.com/”. But, for paths under “example.com/” like “example.com/users” I’d like to omit the trailing slash. Although it would be nice to have it either way - example.com with or without a trailing slash.:lol:

Regards,

DK