Forum Moderators: phranque
I'm trying to do some mod rewrite magic and not having a lot of success.
I have a blog where the links currently look like "site.com/category/topic-title/"
I'm trying to change it to be:
"site.com/category/topic-title.html"
Any suggestions on how to do this? I was thinking either ea 'redirect match' or a 'RewriteRule' but i just can't seem to get either one working.
"RedirectMatch 301 ^/category/([^/]+/)*(.*)$ [mysite.com...]
Is that totally incorrect?
Thanks for any help!
I haven't tested this but something like this should work:
RewriteRule ^category/([A-Za-z0-9]+)/$ http://www.example.com/category/$1.html [R=301,NC]
I use a similar rule to force /category/ to /category (no trailing slash, no extension).
And I also agree that adding the .html extension is counter-productive in several ways: It makes your URLs longer, it dilutes any keyword-in-URL ranking advantage by doing so, and it guarantees that you'll have to change your URLs again in the future if, say, you switch to .php.
Most Webmasters who can do so are currently removing file extensions, not adding them. The only change I'd recommend for a site like you describe is to remove the trailing slashes, since they too are a waste of bandwidth.
Jim
What I wanted to do was make sure the old posts are redirected to the new ones.
I've heard the debate about the file extension and it seems for everyone that says drop them, there are others who say add them.
Have you read anything somewhat 'definitive'? I was just going to do it for consistency.
1. Google doesn’t care about link depth (i.e. the number of slashes in your permalink won’t matter)
2. The file extension doesn’t matter - you could call files as php, html or even mattcutts.. this is not taken into consideration while calculating the rankings but don’t use the .exe extension.
Basically I've gone with JD's suggestion and am gonna drop the file extensions, but keep the category as a directory.
So - I'd like to go from: www.example.com/category/thepage/
to:
www.example.com/category/thepage
I've tried this code (and variations thereof), but can't seem to get it to work..either the redirection doesn't work at all, or it drops the 'category' part, or it changes nothing:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /category-name/[^.]+\/
RewriteRule ^category-name/([^.]+)\/$ http://www.example.com/$1 [R=301,L]
Can anyone help me refine that? I'd appreciate it.
Thanks!
[edited by: jdMorgan at 11:25 pm (utc) on Mar. 12, 2008]
[edit reason] example.com [/edit]
Where did the RewriteCond come from -- And why did you add it?
What is an example URL you are requesting?
What URL do you want that URL to redirect to?
Where is the content for the new URL to be served from, that is, what is the filepath?
Do you have a rule to do a rewrite to implement this previous step?
And lastly, what is the URL-path to this .htaccess file.
Please don't confuse URLs with filepaths in your answer to the questions above, that'll just delay the resolution of the problem.
I suspect that either the code is not where it should be, or perhaps you want to back-reference more of the requested URL, but can't be sure.
Jim
I was trying to use a similar solution as found in this thread:
[webmasterworld.com...]
Example requested URL:
www.example.com/category/thepage/
The url I want it to go to is the same, just drop the trailing slash:
www.example.com/category/thepage
File path should just be 'home/examplesite/category/thepage'
As far as I know there are no other rules affecting this particular rewrite rule. However I'm using wordpress, so its possible that there's something hidden, but I doubt it.
I'm putting the rules in my httpd.conf (as its a dedicated server).
I hope that helps explain things a little bit!
.htaccess code:
RewriteRule ^category-name/([^.]+)/$ http://www.example.com/category-name/$1 [R=301,L]
RewriteRule ^(category-name/[^.]+)/$ http://www.example.com/$1 [R=301,L]
httpd.conf code:
RewriteRule ^/category-name/([^.]+)/$ http://www.example.com/category-name/$1 [R=301,L]
RewriteRule ^/(category-name/[^.]+)/$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(/category-name/[^.]+)/$ http://www.example.com$1 [R=301,L]
Seems to work - for the most part. The final trailing slash is removed, however if I want to go to just /category-name/ the trailing slash on category-name is also removed so it looks like '/category-name'
Is there something we can add to that rewrite rule to remedy that or is it perhaps wordpress messing with things?