Forum Moderators: phranque

Message Too Old, No Replies

check my .htaccess rewritecond, rewriterule?

having a little trouble getting a redirect to work correctly.

         

kolin

7:49 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



in my root directory i have this in my .htaccess file


php_value display_errors on

RewriteEngine On
RewriteOptions MaxRedirects=10

RewriteCond %{HTTP_HOST} ^(www\.)?webshop\.co\.uk$
RewriteCond %{REQUEST_URI} !^/shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /shop/$1 [QSA,L,R=301]

RewriteCond %{HTTP_HOST} ^(www.)?webshop\.co\.uk$
RewriteRule ^(/)?$ shop/index.php [L]

RewriteCond %{HTTP_HOST} ^(www.)?website\.co\.uk$
RewriteRule ^shop/(.*)$ http://www.website.co.uk/$1 [QSA,L,R=301]
RewriteRule ^((urllist¦sitemap¦gss).*\.(xml¦txt¦xsl)(\.gz)?)$$1[L]
RewriteRule ^([^/]*)$ /hide/engine.php?file=$1 [QSA]

basically in the first section (which works) if the domain is webshop then it will redirect to the /shop directory on website.

the second section if it is a straight url of webshop it goes to the index page.

the third section is where my problem lies if a user clicks on an old link (eg www.website.co.uk/shop/c1/this-category) i need it to redirect to www.webshop.co.uk/c1/this-category

i have tested


^shop/(.*)$ http://www.website.co.uk/$1 [QSA,L,R=301]

and it works. however in reality (with all the rewriteconds) i dont think it gets to this stage....

SECONDLY
in the /shop/ directory i have another .htaccess file (below)


php_value display_errors on

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?webshop\.co\.uk$
RewriteRule ^c([0-9]+)/[^/]+_all$ /shop/category.php?cid=$1&tp=1[QSA,L]
RewriteRule ^c([0-9]+)/[^/_]+$ /shop/category.php?cid=$1[QSA,L]
RewriteRule ^p([0-9]+)/[^/]+$ /shop/product.php?pid=$1[QSA,L]
RewriteRule ^brand/([0-9]+)/[^/]+$ /shop/results.php?brand=$1[QSA,L]
RewriteRule ^((urllist¦sitemap¦gss).*\.(xml¦txt¦xsl)(\.gz)?)$$1[L]

all the status bar links and hyperlinks look like this


www.webshop.co.uk/p2/this-product

and all the links work, however in the address bar they show as

www.webshop.co.uk/shop/p2/this-product

how can i stop it showing the 'shop' bit?

g1smd

9:03 pm on Mar 18, 2009 (gmt 0)

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



On a quick look, you need to change the rule order so that all of the redirects are listed before all of the rewrites. Failure to do that will expose internal paths from rewrites if there is a subsequent redirect - something that you do not want to happen. Make sure all the redirects are listed first.

Within each group, you need "most specific" (i.e. affects the least number of URLs) listed first, and 'least specific' (i.e. affects the most number of URLs - catches anything not already actioned by the preceding rules) last.

Every redirect should contain the canonical domain in the target URL, as well as [R=301,L].

Rewrites should not accept URL requests for Duplicate URLs. That is, place a redirect from non-www to www before the rewrite. Don't let the rewrite capture both non-www and www URL requests.

Likewise, don't let trailing slash be optional as input URL to a rewrite. Set up a preceding redirect to 'fix' the URL to one format, before the rewrite kicks in.

You must add a required space before the [FLAGS] in most lines of code above.

Correct the code as detailed above and then post again, so that any remaining issues can be debugged.

[edited by: g1smd at 9:14 pm (utc) on Mar. 18, 2009]

kolin

9:12 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



"Within each group, you need "most specific" (i.e. affects the least number of URL) listed first, and 'least specific' (i.e. affects the most number of URLs - catches anything not already actioned by the preceding rules) last."

i'm not sure i'm following this.

the third section


RewriteCond %{HTTP_HOST} ^(www.)?website\.co\.uk$
RewriteRule ^shop/(.*)$ http://www.website.co.uk/$1 [QSA,L,R=301]
RewriteRule ^((urllist¦sitemap¦gss).*\.(xml¦txt¦xsl)(\.gz)?)$$1[L]
RewriteRule ^([^/]*)$ /hide/engine.php?file=$1 [QSA]

seems to be working. except if i directly enter the url


http://www.website.co.uk/shop/c1/this-category

i just get a 404, however all other links in that section's hierarchy of precedence work. including the last rule

www.website.co.uk/this-page

kolin

9:13 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



the spaces are there, i seemed to have messed up the code tags somehow.

g1smd

9:16 pm on Mar 18, 2009 (gmt 0)

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



*** i'm not sure i'm following this. ***

Your second section contains a rewrite.

Your third section contains a redirect and a rewrite.

You MUST list ALL of the redirects first, before you list ANY of the rewrites.

g1smd

9:21 pm on Mar 18, 2009 (gmt 0)

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



You can re-edit your original post to correct the code tags.

You need to add a single # on each and every line that is currently blank.

g1smd

11:21 pm on Mar 21, 2009 (gmt 0)

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



How are you getting on?