the URLs were redirected (no internal server error this time!) … it means if I visit www.site.com/page-one.php it redirects me to www.site.com/page-one (which is good)
No, it’s not!
but I got “page not found” …
because the resource “yoursite.com/page-one” does not actually exist.
You DON’T want www.yoursite.com/page-one.php to get redirected! What you want, a rewrite, is that IF someone types in
www.yoursite.com/page-one.php
then that’s what they get like before (sorry, however if you change all the hyperlinks in your pages and ads etc then they won’t be going here very often)
BUT
if someone types in (or clicks a link)
www.yoursite.com/page-one
that the user does not see a new url (they see the page-one without the .php like you want), but DOES see the content at
www.yoursite.com/page-one.php
(because this is the real page who actually exists in your system)
So what a rewrite (not redirect) does is make other, prettier URLs actually bring back the real, ugly URL’s resource (page).
it’s rewrite not a redirection as it seems …
Yup, you got it.
So all you want to do for those URLs is something like (example! all mistakes are mine!):
RewriteRule ^([-\w\d]+)(\.php)?$ $1.php [L]
+ is an example representing anything (at least one character of letters, digits or hyphens anyway) after yoursite.com/ and MAY be followed by .php (you wouldn’t want to end up with page-one.php.php) and the $1 only grabs that first set of parens anyway.
I just need to redirect all the www.site.com/example-1/article-title.htm to the new location www.site.com/example-2/article-title
Yup, this one IS a redirect (though also a rewrite).
The redirection is from the folder example-1 to the folder example-2.
The rewrite is from example-2/some-page to whatever the REAL page is (is it example-2/some-page.php??).
I used
Quote:
RewriteEngine on
RewriteBase /
RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]
but when I access an old article at the old URL www.site.com/example-1/article-title.htm nothing happened … no redirection … nothing …
I didn’t see where you escaped the . in the .htm extension.
In the left-hand side of the RewriteRule, you are trying to make a match. This means that special chars (as DK said) need to be escaped.
On the right-hand side of the RewriteRule, that’s your real resource URL. You don’t escape stuff there.
So you have
RewriteRule ^example-1/(.+).htm$ example-2/$1 [R=301,L,QSA]
It’s close. I dunno that you need the QSA, I thought that was default??
Questions:
- Do absolutely ALL the urls in example-1 folder end in .htm?
- What do the new URLS (the real, ugly ones that actually bring up real pages) end with?
If they really end with .php, then you would place this example-1 example-2 line right before the other line above where you just redirect stuff without the .php ending to the real .php page. This means you wouldn’t end the first line with [L] because you do want it to go through the next line too.
You might have already said what all the pages really end in but I’m easily confuzeld and may have missed it… sorry : )
If I screwed anything up too badly, I expect DK to give me a good head-bashing, but I think I’m on the right track.