Forum Moderators: phranque

Message Too Old, No Replies

Redirect permanent any file in directory to new domain same directory

How to

         

silverbytes

8:46 pm on Mar 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to redirect from my old site to new site:

/mydir/wallpapers/myimage1.jpg
/mydir/wallpapers/myimage2.jpg
/mydir/wallpapers/myimage3.jpg

to my new domain in same dir, so when anybody search for
myolddomain/mydir/wallpapers/myimage1.jpg
goes to

mynedomain/mydir/wallpapers/myimage1.jpg

and if somebody search for image2.jpg same thing.

Is there a way to do that with just 1 line in .htacess?

g1smd

10:06 pm on Mar 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes. Actually, it might usually be two lines of code, making one directive.

Check the last few days of postings in this forum. Someone asks the same question almost every day.

Post your best effort code here if you run into problems.

silverbytes

10:14 pm on Mar 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Browsed many days back and couldn't find what I want. (many questions about syntax but not this one)

Ok, would that work?

RewriteRule ^/mydir/wallpapers/?(.*) http://www.mynewdomain/mydir/wallpapers/$1 [R=301,L]

[edited by: jdMorgan at 2:10 am (utc) on Mar. 20, 2008]
[edit reason] de-linked [/edit]

g1smd

10:34 pm on Mar 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not Quite.

RewriteRule cannot "see" the leading "/" of the old URL.

Test it and see.

Use the Live HTTP Headers extension for Mozilla Seamonkey or Mozilla Firefox to see the actual servers responses.

jdMorgan

2:08 am on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Minor corrections:

RewriteRule ^mydir/wallpapers/(.*)$ http://www.mynewdomain/mydir/wallpapers/$1 [R=301,L]

This redirects *anything* in the /mydir/wallpapers/ directory to the new domain.

Jim

[edited by: jdMorgan at 2:09 am (utc) on Mar. 20, 2008]

g1smd

2:16 am on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It might not redirect olddomain.com/mydir/wallpapers (URL without trailing "/") or it might cause a double redirect for that particular URL.

silverbytes

5:55 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is not working at all. I just get a 404 error... tried even
adding the domain before dir too and nothing

RewriteRule ^http://www.mydomain/mydir/wallpapers/(.*)$ [mynewdomain...] [R=301,L]

Tried putting the string at bottom, top and middle. And nothing, allways 404 error.

Is that dependant of any apache version?

jdMorgan

6:12 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code I posted is for use in example.com/.htaccess only.

Do not include the domain in the rule's URL-pattern; It should work exactly as I posted it.

Do you have any other working mod_rewrite rules in this .htaccess file?
If not you'll need to precede the rewriterule with the second line below, or both the first and second line:


Options +FollowSymLinks
RewriteEngine on

You will need to test to find out if you need the first line; Some server configurations require it, some don't, and some won't allow it.

Completely flush (delete) your browser cache before testing any change to server-side code.

Jim

silverbytes

6:37 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have this

RewriteEngine on
RewriteRule ^/simpsons-photos/wallpapers/(.*)$ http://www.example.com.ar/simpsons-photos/wallpapers/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.esp.br(/)?.*$ [NC]
RewriteRule .*\.(gif¦jpg¦jpeg¦bmp¦png)$ - [F,NC]
RewriteCond %{REMOTE_ADDR} ^204\.13\.51\.241$ [OR]
RewriteCond %{REMOTE_ADDR} ^204\.13\.51\.246$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^http://www\.iaea\.org [OR]

I'll try emptying cache...

[edited by: jdMorgan at 6:52 pm (utc) on Mar. 20, 2008]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

7:00 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have added a leading slash to your pattern, so your rule will not work in .htaccess. The take-home lesson here is that mod_rewrite is not to be trifled with: A single error or character out of place can break a rule and/or cause your server to become inaccessible.

Let's clean this up:


RewriteEngine on
#
RewriteRule [b]^sim[/b]psons-photos/wallpapers/(.*)$ http://www.example.com.ar/simpsons-photos/wallpapers/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?exampl[b]e\.esp\.br[/b] [NC]
RewriteRule [b]\.([/b]gif¦jp[b]e?g[/b]¦bmp¦png)$ - [F,NC]
#
RewriteCond %{REMOTE_ADDR} ^204\.13\.51\.241$ [OR]
RewriteCond %{REMOTE_ADDR} ^204\.13\.51\.246$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^http://www\.iaea\.org [OR]
...
(RewriteRule here to block unwelcome access)

That should work better, and a little faster, too.

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

silverbytes

8:01 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now works perfectly. Awsome. Thank you!