Forum Moderators: phranque
/photos/new-york/architecture/page-name
to:
/photos/new-york/architecture/page-name/
The rewrite rule is adding an extra trailing slash to the end of the URL:
/photos/new-york/architecture/page-name//
Here is the code:
RewriteRule ^(photos/)?new-york/architecture/([^.]+)$ http://example.com/photos/new-york/architecture/$2/ [R=301,L]
Am I missing something?
RewriteRule ^(photos/)?new-york/architecture/([^.]+)$ http://example.com/photos/new-york/architecture/$2/ [R=301,L]
I'm suspect of your existing negative group "[^.]" -- as it will reject any URL with a filetype on it. But leaving that for the moment. you can add the slash to the group, so it will also reject any URL with additional slashes following "architecture/":
RewriteRule ^(photos/)?new-york/architecture/[b]([^./]+)[/b]$ http://example.com/photos/new-york/architecture/$2/ [R=301,L]
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(photos/)?new-york/architecture/([^./]+)$ http://example.com/photos/new-york/architecture/$2/ [R=301,L]
In regular expressions, "[^.]+" means, "match one or more characters not equal to a period."
I added the "/", changing the pattern to "[^./]" and making it mean, "match one or more characters not equal to a period or a slash."
That is why it's critical to understand regular expressions and all of the Apache directives you use: Every single character in your .htaccess file can potentially shoot down your site or cause subtle but dangerous (to your income) problems with search engine spiders if it is incorrect. And only you can 'sign off' on the code as being 100% correct for your needs, based on your URLs, on your site. There is no such thing as 'one size fits all' code, and all we can really do here is talk in generalities about the most common applications.
Jim
Unfortunately I didn't discover a major error that the above code caused until this morning. It also adds the trailing slash to page numbers which shouldn't have them...for example it converted:
http://example.com/photos/architecture/2
to
http://example.com/photos/architecture/2/
I understand why it's doing that, because ([^./]+) will aplly to any number or character which is not a period or slash. How would I rewrite this to say "match any character NOT a period, slash, or number"? Very few of my page names have a number in them so that would correct both the first problem and prevent the slash from being added after numbers. I have given it a shot here:
([^./[0-9]]+)
Can this be nested in this way?
Thanks!
RewriteCond %{REQUEST_URI}!/$
RewriteRule ^(photos/)?cityscapes-skylines/([0-9]+)/$ http://example.com/photos/cityscapes-skylines/$2 [R=301,L]
Also I was wondering...wouldn't it be better if the page numbers DID end in a slash? I have read that urls not ending in trailing slashes can cause problems sometimes. Thanks!
# Strip trailing slash from /cityscapes-skylines/<number>/ and /photos/cityscapes-skylines/<number>/ URLs,
# and redirect to example.com/photos/cityscapes-skylines/<number> as the canonical URL
RewriteRule ^(photos/)?cityscapes-skylines/([0-9]+)/$ http://example.com/photos/cityscapes-skylines/$2 [R=301,L]
Also I was wondering...wouldn't it be better if the page numbers DID end in a slash? I have read that urls not ending in trailing slashes can cause problems sometimes. Thanks!
The only reason that the slashless URLs might be preferred is that --in a non-rewritten environment-- the slash approach would require that each slashed 'page' be the index file of its own directory, thus requiring a whole lot of directories with (possibly) only the index file in them. But if you're rewriting URLs, this no longer applies, since URLs and filepaths then become largely independent. Other than that, I prefer the slashless page URLs only because they are one character shorter.
Jim