Forum Moderators: phranque

Message Too Old, No Replies

Dynamic URL has changed, need help with .htaccess

.htaccess help

         

Tamashi

12:45 am on Mar 24, 2010 (gmt 0)

10+ Year Member



I am running a website that heavily depends on traffic trading through a traffic trading script. I have replaced my old traffic script for a new one, but i'm having a slight problem with the old and new urls now.

My old url:
[mydomain.com...]
My new url:
[mydomain.com...]

How can I set this up in htaccess so it correctly sends the old url's to the new one?

Tamashi

12:50 am on Mar 24, 2010 (gmt 0)

10+ Year Member



Please note that "tradedomain.com" never includes [www....] It's just the actual domain name that is used by the script.

g1smd

12:58 am on Mar 24, 2010 (gmt 0)

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



You could have employed a rewrite so that users continue to use the old URLs, the rewrite mapping their incoming URL request to the internal server location where the script actually resides.

However, if you insist on using new URLs out on the web, first change all on-page links to point to the new URLs, then set up a redirect such that anyone asking for the old URLs is redirected to make a new request for the new URL.

Tamashi

1:09 am on Mar 24, 2010 (gmt 0)

10+ Year Member



All URL's have been updated on the pages already, but I still need something that sends old url's to the new ones when requested.

/out.php?trade=tradedomain.com to /tx/out.php?t=tradedomain.com

"tradedomain.com" should stay the same during the rewrite. For example:
/out.php?trade=google.com
should become:
/tx/out.php?t=google.com

I am not looking for lines with hardcoded trade domains, just a simple rewrite that copies the tradedomain from the old link into the new one.

Tamashi

11:54 pm on Mar 24, 2010 (gmt 0)

10+ Year Member



anyone?

g1smd

12:17 am on Mar 25, 2010 (gmt 0)

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



What code have you tried so far? What did it do, and how did that differ from what you expected?

The majority of the 68 000 threads in this sub-forum contain Mod_rewrite code of some sort or another, many thousands of those for redirects of various kinds, if you're stuck for examples.

Tamashi

12:58 am on Mar 27, 2010 (gmt 0)

10+ Year Member



I tried doing this:

RewriteCond %{QUERY_STRING} ^trade=(.*)$
RewriteRule ^out.php$ /tx/out.php?t=$1 [R=301,L]

it redirected to /tx/out.php?t= but it did not copy the url from the last link (.*)$

How can I correct it, so it copies the link in the rewrite link aswell?

jdMorgan

11:50 am on Mar 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The main problem is that you tried to use $1 to back-reference a RewriteCond variable. RewriteConds create "%" back-references, while RewriteRules create "$" back-references. I'd suggest:

RewriteCond %{QUERY_STRING} ^trade=([^&]+)
RewriteRule ^out\.php$ [b]http://example.com[/b]/tx/out.php?t[b]=%1[/b] [R=301,L]

Jim

Tamashi

12:08 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



That's giving me an internal server error for some reason

RewriteEngine on
#
# permanently redirect from non-www domain to www domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.shtml\ HTTP/
RewriteRule ^(.*)index\.shtml$ /$1 [R=301,L]
#
RewriteRule ^(.*)/?gallery/(.*)$ /click.php?%{QUERY_STRING}
#
RewriteRule ^visit/(.*)$ http://www.example.com/tx/out.php?t=$1
#
RewriteRule ^go/(.*)/$ /redirect.php?title=$1 [L]
RewriteRule ^go/(.*)$ /redirect.php?title=$1 [L]
#
# RewriteCond %{QUERY_STRING} ^trade=([^&]+)
# RewriteRule ^out\.php$ http://www.example.com/tx/out.php?t=%1 [R=301,L
#
ErrorDocument 404 /index.shtml


What's wrong, or causing the internet server error? If I remove your code, the error is also gone.

Tamashi

12:12 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



Never mind, the problem was something stupid while copy pasting. Forgot to close [R=301,L

Working now, thanks!

g1smd

6:52 pm on Mar 27, 2010 (gmt 0)

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



There's major issues with the posted code.

You need to list all the redirects before all the rewrites.

The redirects (only the redirects, not the rewrites) should include the protocol and domain name as a part of the target destination.

Within the list of redirects you need to list the most specific first, and the most general (the non-www to www) last.

Within the list of rewrites you need to list the most specific first, and the most general last.

Failure to do so will cause very strange problems for certain non-canonical requests.

The ?gallery rule can NEVER work, because RewriteRule cannot see query strings. Add a RewriteCond that tests %{QUERY_STRING} here. In that rule you also need to find a better, more efficient, pattern than (.*) too.

EVERY rule needs the [L] flag added where it is currently missing.

Tamashi

7:49 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



thanks for your comment, I'll take a look at it. I don't have too much experience with mod_rewrite. The gallery rule does not contain a query string in the url, it is meant to work on both the root and all 47 subdirectories with one single htaccess file.

How does something like this look to you?

RewriteEngine on

#redirects
#
# redirect /out.php?trade= to /tx/out.php?t=
RewriteCond %{QUERY_STRING} ^trade=([^&]+)
RewriteRule ^out\.php$ http://www.example.com/tx/out.php?t=%1 [R=301,L]

# redirect 404 not found to index page
ErrorDocument 404 http://www.example.com/index.shtml

# remove index.shtml from the url everywhere on the site including all subfolders
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.shtml\ HTTP/
RewriteRule ^(.*)index\.shtml$ /$1 [R=301,L]

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


# rewrites
#
# rewrite gallery urls in all 42 subfolders to the click.php script | example: http://www.example.com/subfolder/gallery-description.html?id=gallery_id&url=gallery_url
RewriteRule ^gallery/(.*)$ http://www.example.com/click.php?%{QUERY_STRING} [L]

# rewrite rewrite /tx/out.php?t=example.com to /visit/example.com
RewriteRule ^visit/(.*)$ http://www.example.com/tx/out.php?t=$1 [L]

# script to hide affiliate links - rewrite example.com/redirect.php?title=example to /go/example
RewriteRule ^go/(.*)/$ http://www.example.com/redirect.php?title=$1 [L]
RewriteRule ^go/(.*)$ http://www.example.com/redirect.php?title=$1 [L]

g1smd

9:22 pm on Mar 27, 2010 (gmt 0)

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



Your 'index redirect' rule is still missing the following...

The redirects (only the redirects, not the rewrites) should include the protocol and domain name as a part of the target destination.

Additionally, all of the things that you have marked as 'rewrites' are now coded as 302 redirects.

The ErrorDocument directive MUST NOT contain a protocol and domain domain name. If you include those things it will produce a 302 redirect, not the 404 you wanted. Additionally, it is better if your ErrorDocument has different content to the homepage. It needs to explain why the user isn't seeing the content they expected, and should link to the main sections and selected useful pages of your site.

Tamashi

9:46 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



Could you please post a preview fix of the issues that are left? How do you code the protocol and the domain with the index redirects, and still have it working on the root AND all subfolders?

And how would you fix the rewrites? My experience with htaccess is close to zero, and I would just like to have this done so I can continue focusing on my websites.

Tamashi

9:53 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



The index redirect has been tested a fixed, then it's just the "302" redirects left, what should be modified there?

Tamashi

11:22 pm on Mar 27, 2010 (gmt 0)

10+ Year Member



Here is my final attempt

RewriteEngine on

# redirects
RewriteCond %{QUERY_STRING} ^trade=([^&]+)
RewriteRule ^out\.php$ http://www.example.com/tx/out.php?l=404+Error&ss=404 [R=301,L]

RewriteRule ^toplist/?(.*)?$ http://www.example.com/tx/register.php [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.shtml\ HTTP/
RewriteRule ^(.*)index\.shtml$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# rewrites
RewriteRule ^gallery/(.*)$ /click.php?%{QUERY_STRING} [L]

RewriteRule ^visit/(.*)$ /tx/out.php?t=$1 [L]

RewriteRule ^go/([^/]+)/?$ /redirect.php?title=$1 [L]

jdMorgan

2:00 am on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Minor tweaks to each rule, except for the last two:

# redirects
RewriteCond %{QUERY_STRING} ^trade=([^&]+)
RewriteRule ^out\.php$ http://www.example.com/tx/out.php?l=404+Error&ss=404 [R=301,L]
#
RewriteRule ^toplist(/.*)?$ http://www.example.com/tx/register.php [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.shtml\ HTTP/
RewriteRule ^(([^/]+/)*)index\.shtml$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# rewrites
RewriteRule ^gallery/(.*)$ /click.php [L]
#
RewriteRule ^visit/(.*)$ /tx/out.php?t=$1 [L]
#
RewriteRule ^go/([^/]+)/?$ /redirect.php?title=$1 [L]

I don't know what you are doing with those mentions of "404" in the first rule. But be very sure that you are not 301-redirecting to out.php, and then producing a 404 response in the out.php script... If this is the case, then this rule should probably be changed to an internal rewrite and be moved down into the rewrite section in the second half of your file.

Jim

Tamashi

9:20 am on Mar 28, 2010 (gmt 0)

10+ Year Member



Thank you! and no, the 404's are identifiers for the traffic script, they do not serve as anything else except knowing the source of the traffic.

Tamashi

9:59 am on Mar 28, 2010 (gmt 0)

10+ Year Member



jdMorgan, for some reason your index.shtml code does not work:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.shtml\ HTTP/
RewriteRule ^(([^/]+/)*)index\.shtml$ http://www.example.com/$1 [R=301,L]

i get sent to example.com/index.shtml instead of example.com/

Tamashi

10:07 am on Mar 28, 2010 (gmt 0)

10+ Year Member



the non-ww to www doesn't seem to work either.

Tamashi

10:12 am on Mar 28, 2010 (gmt 0)

10+ Year Member



never mind, it was my own fault. For some reason the RewriteEngine On line wasn't in the file anymore, thanks again!