Forum Moderators: phranque

Message Too Old, No Replies

Rewrite old URLs to new subdomain

URLs rewrite

         

Egycode

9:53 am on Dec 7, 2009 (gmt 0)

10+ Year Member



Hello,

I want to rewrite URLs to a new subdomain,

Rewrite or redirect with or without www main domain when it end with play=** ,

[mydomain.com...]

or

[mydomain.com...]

to

[subdomain.mydomain.com...]

Thanks.

g1smd

10:14 am on Dec 7, 2009 (gmt 0)

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



Redirects and rewrites are completely different things. You are going to need to clarify which you want, and it is possible you'll need both.

A redirect tells the browser to make a new request for a new URL.

A rewrite connects a URL request with an internal filepath in the server, so its target is NOT a URL, just a filepath (therefore it will not have a domain name in the rule).

Additionally, you need to provide your code for discussion as this forum is not a free code-writing service. There aren't enough volunteers.

The good news is that your question, or some small variant of it, is asked almost every day, so there are hundreds of prior threads with example code in.

Egycode

10:27 am on Dec 7, 2009 (gmt 0)

10+ Year Member



Hello,

What do you mean by provide code for discussion?

I want to redirect visitors and specially Google cached links to the new subdomain where the Flash arcade present,

i.e redirect http://example.com/index.php?play=someid

or

http://www.example.com/index.php?play=someid

to

[games.example.com...]

I have this in my .htaccess which i use to redirect visitors from old domain name to the arcade subdomain,

RewriteCond %{HTTP_HOST} ^(www\.)?example2\.net
RewriteRule ^(.*) [games.example.com...] [R=301,L]

Sorry for any inconvenience this may cause to you.

Thanks.

[edited by: jdMorgan at 3:43 pm (utc) on Dec. 7, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:42 pm on Dec 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> What do you mean by provide code for discussion?

How have you tried to solve your own problem? Post your best-effort at coding a solution. We will help you get your code working. We cannot write your code for you, as that is not the purpose of this forum.

Jim

Egycode

7:09 pm on Dec 8, 2009 (gmt 0)

10+ Year Member



Sorry for my late response, but I was searching and playing with codes.

Still have problem The page isn't redirecting properly

This is my code in the htaccess file
Options -Indexes
IndexIgnore *
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
rewriteCond %{QUERY_STRING} play [NC]
rewriteRule ^.*$ [games.example.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.net
RewriteRule ^(.*) [games.example.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9-]+\.)*example.com/index.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9-]+\.)*example.com/warn.swf [NC]
RewriteRule .*\.(swf)$ /warn.jpg [R,NC]
#RewriteRule .*\.(jpg¦jpeg¦gif¦png¦bmp¦swf)$ [R=301,NC]
#RewriteRule .*\.(mp3¦mpeg¦mpg¦ram¦rm¦wma¦wav¦asx¦wmv¦avi¦mov¦zip¦rar¦exe)$ [R=301,NC]

Thanks.

[edited by: jdMorgan at 10:24 pm (utc) on Dec. 8, 2009]
[edit reason] example.com [/edit]

jdMorgan

10:22 pm on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In several or all of these rules, it will be necessary to use a RewriteCond to test for "NOT the path that this rule rewrites to" in order to prevent an infinite loop. For example, the last two commented-out rules likely failed because you specified that you wanted to redirect *any* .jpg file request to a .jpg file. So that redirect took place, the client came back asking for "warn.jpg" and the code redirected it again to "warn.jpg" So you need to add

RewriteCond %{REQUEST_URI} !^/warn\.jpg$

Your first rule likely fails because you did not remove the query string, so it is still there after the redirect, so you'll get another redirect. End the RewriteRule substitution URL with "/?"

Do yourself a huge favor: Put a blank line or a a comment line between each of your rule-sets. and add comments describing exactly what each rule-set is intended to accomplish.
to stop that loop.

Jim

Egycode

11:00 am on Dec 9, 2009 (gmt 0)

10+ Year Member



Hello, thank you for your concern.

I divided each code in my post

Options -Indexes
IndexIgnore *
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
## this code for redirecting to subdomain.domain.com when any URI which have domain.com/index.php?play= ##
rewriteCond %{QUERY_STRING} play [NC]
rewriteRule ^.*$ [games.example.com...] [R=301,L]
##

## this code to redirect from old domain to my subdomain.domain.com ##
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.net
RewriteRule ^(.*) [games.example.com...] [R=301,L]
##

## this code to redirect non www to www ##
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
##

## this code to prevent swf flash files hotlinking from my site ##
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9-]+\.)*example.com/index.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9-]+\.)*example.com/warn.swf [NC]
RewriteRule .*\.(swf)$ /warn.jpg [R,NC]
#RewriteRule .*\.(jpg¦jpeg¦gif¦png¦bmp¦swf)$ [R=301,NC]
#RewriteRule .*\.(mp3¦mpeg¦mpg¦ram¦rm¦wma¦wav¦asx¦wmv¦avi¦mov¦zip¦rar¦exe)$ [R=301,NC]
##

Also regarding you code,

RewriteCond %{REQUEST_URI} !^/warn\.jpg$

I want to prevent hotlinking of swf flash files from my site, so I redirect to warn.jpg.

My code is wrong?

RewriteRule .*\.(swf)$ /warn.jpg [R,NC]

Thanks.

jdMorgan

2:29 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You left two additional commented-out rules at the end that implied that you might also want to block hotlinking of other filetypes. I was warning you that you must add an exclusion to the rule if you want to block the ".jpg" filetype, otherwise, the rule would create an 'infinite' redirection loop, because it would redirect to a filetype that it would redirect... again and again.

Fixing the looping problem in the first rule, removing unnecessary directives, and cleaning up several minor errors, omissions, and inefficiencies, I'd suggest:


Options +FollowSymLinks -Indexes
RewriteEngine on
#
# redirect any URI with "play=" query to subdomain.example.com/
RewriteCond %{QUERY_STRING} [b]^([^&]*&)*play=[/b] [NC]
RewriteRule ^ http://games.example.co[b]m/?[/b] [R=301,L]
#
# redirect from old domain to subdomain.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.net [b][NC][/b]
RewriteRule ^(.*)$ http://games.example.com/$1 [R=301,L]
#
# redirect non-www to www, remove FQDN mark and/or port number from www
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
[i]RewriteCond %{HTTP_HOST} ^www\.example\.com(\.¦\.?:[0-9]+)$ [NC][/i]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# prevent most hotlinking of swf flash files
RewriteCond %{HTTP_REFERER} !=""
RewriteCond %{HTTP_REFERER} !^http://[b]([^.:/]+\.)*example.com/[/b] [NC]
RewriteRule [b]\.s[/b]wf$ [i]http://www.example.com[/i]/warn.jpg [NC,R=302]

Important: Replace the broken pipe "¦" character with a solid pipe before use; Posting on this forum modifies the pipe characters.

Jim

Egycode

3:47 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



Hello,

Thank you very much for the great help man! the codes working perfectly now.

One more question please,

I want to redirect any page with example.com/index.php?play=44 to games.example.com/index.php?play=44 , i.e the same game id not only to the subdomain,

# redirect any URI with "play=" query to subdomain.example.com/
RewriteCond %{QUERY_STRING} ^([^&]*&)*play= [NC]
RewriteRule ^ http://games.example.com/? [R=301,L]

But your help is really appreciated and I have to say big thanks.

[edited by: jdMorgan at 6:10 pm (utc) on Dec. 9, 2009]
[edit reason] Disabled smilies [/edit]

Egycode

4:04 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



Sorry for updating the topic twice,

I want to rewrite the swf file, for example I want to redirect to the same swf file name but with warn swf file, i.e example.swf original size say 400KB but when trying to download it it redirect to the same file name but with another swf file on the server like warn.swf.

Sorry for posting this link but this is what I'm looking for,

<snip>

Try to dowmload this file and you will get the file with the same name but corrupted and small in size!

I forgot to say I that the page load became more faster than before after you corrected the .htaccess codes.

Thanks.

[edited by: jdMorgan at 6:08 pm (utc) on Dec. 9, 2009]
[edit reason] No links, please. [/edit]

Egycode

4:13 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



Regarding my request to redirect to subdomain with the smae requested game id, I have edited the htaccess code and it works now,

# redirect any URI with "play=" query to subdomain.example.com/
RewriteCond %{QUERY_STRING} ^([^&]*&)*play= [NC]
RewriteRule ^ http://games.example.com/$1 [R=301,L]

Please review my other question in my previous post and sorry for adding my site name in the code, didn't saw it.

Thanks.

[edited by: jdMorgan at 6:10 pm (utc) on Dec. 9, 2009]
[edit reason] example.com, removed smilies. [/edit]

jdMorgan

6:20 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# serve short substitute file for hotlinked swf flash requests
RewriteCond %{REQUEST_URI} !^/warn\.swf$
RewriteCond %{HTTP_REFERER} !=""
RewriteCond %{HTTP_REFERER} !^http://([^.:/]+\.)*example.com/ [NC]
RewriteRule \.swf$ /warn.swf [NC]

I'd suggest that you create warn.swf as a nice, short-but-attractive ad for your own site, and make it clickable. Anyone who then hotlinks your media becomes an advertiser and free traffic source for your site.

Otherwise, you'd might as well just send a 403-Forbidden response and be done with it; There is no use saying "Ha ha, I caught you" or anything that's either 'cute' or mean, because the person viewing the page is not the person to blame for the hotlinking... Any explicit "challenge" to the hotlinker himself may only increase his focus on your site. So be cool about handling this... very cool.

Jim

Egycode

6:58 pm on Dec 10, 2009 (gmt 0)

10+ Year Member



Thank you for your help and I appreciate your advise regarding hotlinking.

Have a nice time Mr. Jim.

Egycode

8:52 pm on Dec 10, 2009 (gmt 0)

10+ Year Member



Hello,

I have new request please,

I want to edit the code to redirect to the subdomain if there is cat,comments or any other query in the URI!

# redirect any URI with "play=" query to subdomain.example.com/
RewriteCond %{QUERY_STRING} ^([^&]*&)*play= [NC]
RewriteRule ^ [games.example.com...] [R=301,L]

Your help will be appreciated.

Thank you in advance.

[edited by: jdMorgan at 2:52 pm (utc) on Dec. 11, 2009]
[edit reason] Disabled smilies in code. [/edit]

Egycode

9:06 pm on Dec 10, 2009 (gmt 0)

10+ Year Member



Sorry again, I found the code,

RewriteCond %{QUERY_STRING} ^([^&]*&)*(play=¦cat=¦action=Comments) [NC]
RewriteRule ^ http://games.example.com/index.php$1 [R=301,L]

What is your opinion about this code!

##RewriteCond %{QUERY_STRING} ^.*(play=¦cat=¦action=Comments).* [NC]

This will do the same effect as your code?

Thanks in advance.

[edited by: jdMorgan at 2:52 pm (utc) on Dec. 11, 2009]
[edit reason] example.com, disabled smilies in code. [/edit]

g1smd

9:25 pm on Dec 10, 2009 (gmt 0)

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



The .* at both ends are not required.

The leading .* will slow your code down a LOT if you leave it in.

jdMorgan

10:29 pm on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING} ^([^&]*&)*(play¦cat)=¦action=Comments&? [NC]

would be most efficient in this case.

As noted above, replace the broken pipe "¦" character with a solid pipe before use.

Jim

Egycode

11:11 pm on Dec 10, 2009 (gmt 0)

10+ Year Member



Thank you both for your help,

If I want to add more action query like action=register and so on to the code,

RewriteCond %{QUERY_STRING} ^([^&]*&)*(play¦cat)=¦action=Comments&? [NC]

It may be like this, or it will slow down my script!

RewriteCond %{QUERY_STRING} ^([^&]*&)*(play¦cat)=¦action=Comments¦action=register¦action=profile&? [NC]

And what is &? for in the code please? I want to learn.

Thanks

[edited by: jdMorgan at 2:53 pm (utc) on Dec. 11, 2009]
[edit reason] Disabled smilies in code. [/edit]

jdMorgan

2:48 pm on Dec 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a look at our Forum Charter, and follow the link to the regular-expressions tutorial. It will help you to write more-efficient patterns, and give you a 'technical' answer to your question about "&?".

The basic idea is to not repeat any part of the pattern that can be re-used, so as to avoid repetetive and wasteful re-parsing of 'common' strings:


RewriteCond %{QUERY_STRING} ^([^&]*&)*(play¦cat)=¦action=(Comments¦register¦profile)&? [NC]

Although the regex tutorial will give you the "meaning" of the "([^&]*&)*" and "&?" sub-patterns, it really won't tell you why I included them. The answer to that is that these sub-patterns serve as "soft anchors" - where "anchoring" is a concept described in that regex tutorial.

The purpose is to prevent unwanted matches on queries like "newaction=play" or "action=player-test".

This may not seem important right now, but as your site grows and or changes, you wouldn't want to cause yourself (or someone else) a problem due to an unexpected match on a new "action" function or a new query value. I'm basically following (and promoting) the idea that regex patterns should be as specific as possible to avoid unexpected matches, now and in the future.

Jim

Egycode

3:13 pm on Dec 11, 2009 (gmt 0)

10+ Year Member



Thank you again for your help Mr. Jim.

Sure I will browse you great forum to learn new stuff, really I have a great interest to surf your site to search for good and valuable threads.

Thank you for your valuable time.

Egycode

4:12 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Hello,

Please I discovered an error from the .htaccess recently, may be I didn't noticed before because pages was saved in the temp internet files!

This is the code;

Options +FollowSymLinks -Indexes
RewriteEngine on
#
# redirect any URI with "play=" query to subdomain.example.com/
RewriteCond %{QUERY_STRING} ^([^&]*&)*(play¦cat)=¦action=(Comments¦register¦profile)&? [NC]

RewriteRule ^ [games.domain.com...] [R=301,L]

# redirect from old domain to subdomain.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.net [NC]
RewriteRule ^(.*)$ [games.domain.com...] [R=301,L]

The error is,

when I call a page from the old domain including play suffix like this,

[olddomain.net...] it redirect to this,

[games.domain.com...] without index.php

So I want to include index.php in the .htaccess code, and also I want to redirect all incoming traffic without index.php to index.php with single htaccess code?

I hope I has been addressed all queries successfully.

Thanks.

Egycode

4:17 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Also I want to restrict viewing SWF flash files to the script using index.php only, I use this code now and I ask you to review it please,

# serve short substitute file for hotlinked swf flash requests
RewriteCond %{REQUEST_URI} !^/warn\.swf$
RewriteCond %{HTTP_REFERER} !=""
RewriteCond %{HTTP_REFERER} !^http://([^.:/]+\.)*g247g.com/index.*$ [NC]
RewriteRule \.swf$ /warn.swf [NC]

When I add index.php$ it don't work and i can hotlink swf files and even downloading them by download accelerators?

Thanks.

Egycode

4:22 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Well I tried this and it solved the redirect without index.php issue,

Options +FollowSymLinks -Indexes
RewriteEngine on
#
# redirect any URI with "play=" query to subdomain.example.com/
RewriteCond %{QUERY_STRING} ^index.php([^&]*&)*(play¦cat)=¦action=(Comments¦register¦profile)&? [NC]

RewriteRule ^ [games.domain.com...] [R=301,L]

Please review this and I still ask to redirect all incoming traffic to URI with index.php at the end.

Thanks.

[edited by: jdMorgan at 10:05 pm (utc) on Dec. 31, 2009]
[edit reason] Turned off smiley-faces in code. [/edit]