Forum Moderators: phranque
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
Thanks.
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.
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]
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]
RewriteCond %{REQUEST_URI} !^/warn\.jpg$
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
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.
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]
Jim
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]
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]
# 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]
# 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]
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
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]
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]
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]
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]
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
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.
# 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.
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]