Forum Moderators: phranque

Message Too Old, No Replies

Simple .htaccess issue

         

Merlin2000

5:35 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



I am using the following .htaccess code to redirect someone to a new domain, as well as redirect 3 old files to a specific file on the new domain name.

RewriteEngine on
RedirectPermanent /~some-user/file1.html http://www.example.com/another-file/
RedirectPermanent /~some-user/file2.html http://www.example.com/another-file2/
RedirectPermanent /~some-user/file3.html http://www.example.com/another-file3/
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} old-domain\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The redirect from the old domain to the new one works, but the specific files are redirected to http://www.example.com/~some-user/file3.html etc.

[edited by: jdMorgan at 7:46 pm (utc) on Feb. 11, 2008]
[edit reason] example.com [/edit]

jd01

3:12 am on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would guess since you are using two different modules, Apache is getting confused. EG you are turning on the RewriteEngine Mod_Rewrite, then have Mod_Alias Redirect code before you have your Mod_Rewrite rules...

If the Redirects are correct, you should be able to fix the issue with a simple change:

RedirectPermanent /~some-user/file1.html http://www.example.com/another-file/
RedirectPermanent /~some-user/file2.html http://www.example.com/another-file2/
RedirectPermanent /~some-user/file3.html http://www.example.com/another-file3/

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} old-domain\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

** The middle two lines (%{HTTP_HOST} conditions) are probably unnecessary since you are directing all requests, unless you know you need them.

(Please, note: I DO NOT USE Mod_Alias, AKA RedirectPermanent, so you could have Greek there and I would think the code was correct)

Hope this helps.

Justin

[edited by: jdMorgan at 7:48 pm (utc) on Feb. 11, 2008]
[edit reason] example.com [/edit]

Merlin2000

3:15 am on Jun 22, 2005 (gmt 0)

10+ Year Member



Just tried that code, thanks :)

For some reason it still redirects all pages going to old-domain.com/~folder/file.mp3 to example.com

But I want it to redirect to example.com/another-folder/some-file.htm

[edited by: jdMorgan at 7:48 pm (utc) on Feb. 11, 2008]
[edit reason] example.com [/edit]

jd01

4:35 am on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



old-domain.com/~folder/file.mp3

The RedirectPermanent only matches ~some-user/file1.html, so unless you have changed the extension from the posts, a .mp3 file will only 'match' the 'catch all' (.*) of the RewriteRule.

You can:

a.) change the RedirectPermanent to the correct version.
b.) use mod_rewrite to redirect all the files EG

RewriteEngine on
RewriteRule ^~some-user/file1\.mp3$ http://www.example.com/another-file/ [R=301,L]
RewriteRule ^~some-user/file2\.mp3$ http://www.example.com/another-file2/ [R=301,L]
RewriteRule ^~some-user/file3\.mp3$ http://www.example.com/another-file3/ [R=301,L]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Please, note:
The order of the rules or redirects must be: files you want to send to a specific directory, then the 'catch all'

The specific files, must match exactly the path to the file or the Redirect or RewriteRule will fail and they will be redirected by the 'catch all'

Justin

[edited by: jdMorgan at 7:50 pm (utc) on Feb. 11, 2008]
[edit reason] example.com [/edit]

Merlin2000

4:47 am on Jun 22, 2005 (gmt 0)

10+ Year Member



Great that seems to work. I would really like to learn this .htaccess stuff, but the docs on it never seem to be that user friendly. Any suggestions how to learn other than asking these kind of questions here?

Someone won't remove broken links to mp3 files from their site, so this will now redirect them to another page saying the previous page won't remove the link. :)