Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirecting simple dynamic query

         

hotlava

7:14 pm on Jul 24, 2010 (gmt 0)

10+ Year Member



I want to redirect:
"http://www.domain.com/deals?url=any/dynamic/query" and
"http://www.domain.com/deals.php?url=any/dynamic/query"

To:
"http://www.domain.com/deals/any/dynamic/query"


This is my .htaccess file. What should I change it to?

Options +FollowSymlinks
RewriteEngine on
Rewritebase /

#php_flag display_startup_errors on
#php_flag display_errors on
#php_flag html_errors on

RewriteRule ^(admin|images) - [L]

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

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [L,R=301]

g1smd

8:04 pm on Jul 24, 2010 (gmt 0)

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



Always list all redirects before any of the rewrites.

Make sure the last redirect is the non-www to www canonical redirect.

Failure to do that will either expose internal server filepaths back out onto the web as URLs or will result in an unwanted multi-step redirection chain or both.

The present rewrite code allows URL requests both with and without a trailing slash to resolve to content. That's a serious Duplicate Content issue.

It's better if the "with slash" URL requests were redirected to the "without slash" version of the URL.

Back to your original question, you'll need a couple of extra redirects and those will need to go ahead of all of your existing code. However, you must also fix all of the other problems first.

hotlava

5:50 pm on Jul 25, 2010 (gmt 0)

10+ Year Member



Not too sure how to fix it.

hotlava

7:03 pm on Jul 27, 2010 (gmt 0)

10+ Year Member



Can anyone explain what g1smd meant to do. I have no idea how to fix it. I have a slight idea on what he is referring to, but I can't see how it messes anything up. htaccess files are not my forte.

jdMorgan

2:28 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but I can't see how it messes anything up.

Try requesting domain.com/index and watching your server's response with a server headers checker. You will see that your request gets redirected twice. That is slow for the user, results in three access log entries, skewing your stats, and search engines will only fully "follow" a single redirect. If you care about passing page rank through the redirect, better make sure there's only one. This problem can be easily fixed by reversing the first two rules.

There are other problems as well, caused by the rules being in the incorrect order.

Jim

hotlava

5:47 am on Jul 30, 2010 (gmt 0)

10+ Year Member



Thanks Jim.

I was wondering when you'd post an answer. You are the ultimate htaccess help on the internet, I must say.

Here is my htaccess file after Jim's changes:

Options +FollowSymlinks
RewriteEngine on
Rewritebase /

#php_flag display_startup_errors on
#php_flag display_errors on
#php_flag html_errors on

RewriteRule ^(admin|images) - [L]

RewriteCond %{the_request} ^[A-Z]{3,9}\ /.*index\ HTTP/ [NC]
RewriteRule ^(.*)index [example\.com...] [L,R=301]
RewriteRule ^(.*)index/$ [example\.com...] [L,R=301]

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

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


So how do I effectively redirect:
"http://www.exmaple.com/deals?url=any/dynamic/query" and
"http://www.example.com/deals.php?url=any/dynamic/query"

To:
"http://www.example.com/deals/any/dynamic/query"

[edited by: jdMorgan at 1:17 am (utc) on Jul 31, 2010]
[edit reason] example.com [/edit]

jdMorgan

1:53 am on Jul 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You still have rules out of order. Put all redirects (those with [R=30n] flags) first.

#php_flag display_startup_errors on
#php_flag display_errors on
#php_flag html_errors on
#
Options +FollowSymlinks
RewriteEngine on
Rewritebase /
#
# Skip all of the following rules for /admin and /images requests
RewriteRule ^(admin|images) - [L]
#
# Redirect direct client requests for /deals?url=dynamic/query to /deals/dynamic/query
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /deals(\.php)?\?url=([^&#\ ]+)
RewriteRule ^deals(\.php)?$ http://www.example.com/deals/%2? [R=301,L]
#
# Redirect direct client requests for "/index" or "/index/" or
# "/index.php" in any directory to "/" in that same directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]*/)*index(/|\.php)?\ HTTP/
RewriteRule ^(([^/]*/)*)index(/|\.php)?$ http://www.example\.com/$1 [R=301,L]
#
# Redirect direct client requests for .php URLs to extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.]+\.)+php\ HTTP
RewriteRule ^(.+)\.php$ http://www.example.com/$1? [R=301,L]
#
# Redirect non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite extensionless URL requests to .php filepaths
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1.php [L]
#
# (efficiency tweak - exclude requests with filetypes appended from 'exists' check)
RewriteCond $1 !\.[a-z][0-9]?$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])$ /$1.php

Jim

[added] Corrected typos as noted below. [/added]

[edited by: jdMorgan at 2:28 pm (utc) on Jul 31, 2010]

hotlava

11:27 am on Jul 31, 2010 (gmt 0)

10+ Year Member



thanks. it works like a charm. I did make two changes:

I changed a ',' into a '.' in line:
RewriteRule ^deals(\.php)?$ [www,example.com...] [R=301,L]

I added 'www.' to the url in line:
RewriteRule ^(([^/]*/)*)index(/|\.php)?$ [example\.com...] [R=301,L]

jdMorgan

12:06 pm on Jul 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well-spotted! -- My old eyes and/or shaky fingers acting up again... :(

I incorporated your corrections into my post above, so that these errors won't propagate further.

Jim

hotlava

1:46 pm on Jul 31, 2010 (gmt 0)

10+ Year Member



I just noticed there is an internal error with the deals redirect.

It redirects:
/deals?url=queens/new-york/2010/07/30/%2450-for-2-hours-of-handyman-services-from-bestco-general-electric-%28%24100-value%29

to:
/deals/queens/new-york/2010/07/30/%252450-for-2-hours-of-handyman-services-from-bestco-general-electric-%2528%2524100-value%2529%20HTTP/1.1?url=queens/new-york/2010/07/30/%252450-for-2-hours-of-handyman-services-from-bestco-general-electric-%2528%2524100-value%2529

Also, how do you suggest I use the '$', '(', and ')' characters in the url? That url looks messy.

jdMorgan

2:28 pm on Jul 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, and there's another problem...

Change the pattern in the RewriteCond to "^[A-Z]+\ /deals(\.php)?\?url=([^&#\ ]+)" and add a question mark to the RewriteRule substitution as shown in the amended code above.

Jim