Forum Moderators: phranque

Message Too Old, No Replies

please help review my htaccess

         

Buster13

2:51 am on Nov 24, 2009 (gmt 0)

10+ Year Member



i did my homework and with advice of some helpful angels from this forum, below is my completed working htaccess on my website. i just wish some experts could review my htaccess and tweak it for better.

Thank you in advance! ;p
==========================================================

RewriteEngine on

#Redirect domain.com to www.domain.com:
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ [mywebsite.com...] [L,R=301]

#Add a trailing slash to requested URLs
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ [mywebsite.com...] [L,R=301]

#RemoveHandler .html .htm
#AddType application/x-httpd-php .php .htm .html
# AddHandler application/x-httpd-php5 .php
AddHandler application/x-httpd-php5 .html
AddHandler application/x-httpd-php5 .htm

ErrorDocument 404 /shop/error.php
ErrorDocument 401 /shop/401.php

RewriteCond %{HTTP_HOST} ^mywebsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mywebsite.com$
RewriteRule ^shop/buy-apparel-clothing-shirts.html$ "http\:\/\/mywebsite\.com\/shop\/" [R=301,L]

### 1st level - FOR HE ¦ FOR SHE

#FOR HE - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/
RewriteRule ^[Ss]hop/[Mm]ens/$ /shop/index.php?c=2 [L]

RewriteRule ^[Ss]hop/[Ww]omens/$ /shop/index.php?c=3 [L]

### 2nd level - Clothes Type: T-shirts ¦ Tops ¦ Polo

#[Tt]-shirts - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/[Tt]-shirts/
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/$ /shop/index.php?c=13 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/$ /shop/index.php?c=15 [L]

### 3rd level Clothes Type > Brands: TwoPlay

#Inbox - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/[Tt]-shirts/44/
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/([0-9]+)/$ /shop/index.php?c=$1 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/([0-9]+)/$ /shop/index.php?c=$1 [L]

### Product URL Clothes Type > Brands: TwoPlay > Product ID

#Houston - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/[Tt]-shirts/44/1187/
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]

### Paging /p2/ /p3/ /p4/ ...

RewriteRule ^[Ss]hop/p([0-9]+)/$ /shop/index.php?page=$1&c=0 [L]

### 1st level - FOR HE ¦ FOR SHE

RewriteRule ^[Ss]hop/[Mm]ens/p([0-9]+)/$ /shop/index.php?page=$1&c=2 [L]

RewriteRule ^[Ss]hop/[Ww]omens/p([0-9]+)/$ /shop/index.php?page=$1&c=3 [L]

### 2nd level - T-shirts ¦ Tops ¦ Polo

RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=13 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=15 [L]

### 3rd level - TwoPlay

# [mywebsite.com...]
RewriteRule ^[Ss]hop/[Mm]ens¦[Ww]omens/[a-zA-z]+/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

jdMorgan

2:55 pm on Nov 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first and second rules are reversed. Put you most-specific external redirect rules first, least-specific redirects later, followed by most-specific internal rewrites, then least-specific internal rewrites.

You code would be easier to read and maintain if you organized it by module. Since directive execution is per-module, it makes no difference whether you declare your ErrorDocuments and MIME-Types in the middle of the mod_rewrite code, or before or after. However, declaring them in the middle as you've done here does tend to break up the 'flow' of reading your rewriterules and may leave the later ones scrolled off-screen while you're tweaking the earlier ones (or vice-versa), leading to errors.

Your third rule could do with some efficiency improvements. All you need is:
[code]RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com
RewriteRule ^shop/buy-apparel-clothing-shirts\.html$ [mywebsite.com...] [R=301,L]
[code]

It is not at all clear why you wish to redirect to this "/shop" URL-path and 'expose' this "/shop" part of the path as a URL. It is also ill-advised to invoke multiple redirects on the same request. That is, if you truly have a good reason to do this redirect to "/shop", then your domain canonicalization rule should also redirect to "/shop" so that clients won't encounter two consecutive redirects if they request a non-canonical domain.

There are several additional 'lessons' in this one tweaked rule as compared to the original:
1) Use the power of regular expressions to write more-compact code.
2) Escape literal periods in regex patterns.
3) Do not end-anchor hostnames unless your pattern can account for FQDN-formatted hostnames -- and with or without a port number appended. For example, this is a perfectly-valid hostname which your original rule would fail to handle properly: www.example.com.:80
4) There is no need to quote or escape substitution URLs in the RewriteRule. They are literal strings except for back-reference tokens $1-$9 and %1-%9. The only characters that need to be escaped are therefore literal "$" and "%" characters, and the use of these literal characters is thankfully quite rare (and ill-advised).

It is a mistake to allow more than one URL to directly-access the same content. Your rules for mens' and womens' t-shirt shopping allow eight different URLs to 'compete' with each other for ranking on the exact same content. It's not likely that you'd enjoy having an additional eight competitors for your business, so why compete with yourself? Pick one single case-variation of your URLs, link only to that URL-type, and redirect all other case variations of that URL to the correctly-cased canonical URL.

The situation with "TwoPlay" is even worse as it accepts a potentially-unlimited number of arbitrarily-cased letters in the third URL-path part. Further, this part of the URL-path is not passed to your script for validation. As a result, an inspired competitor could kindly buy some links for you and point them to your site -- Links such as example.com/shop/mens/very-shoddy-quality-shoes/18/p2, and cause your site to become associated with those terms in search...

There are likely more improvements you could make here, I've only tried to touch on a few.

Jim

Buster13

3:13 am on Nov 25, 2009 (gmt 0)

10+ Year Member



Thank for the great review. Below are my changes to your comments, and lastly, my new modified htaccess to your advices.

===========================================================

[The first and second rules are reversed. Put your most-specific external redirect rules first, least-specific redirects later, followed by most-specific internal rewrites, then least-specific internal rewrites.]

I have reversed the order of first and second rules.
I have re-arrange by most-specific internal rewrites then least-specific internal rewrites.

[You code would be easier to read and maintain if you organized it by module. Since directive execution is per-module, it makes no difference whether you declare your ErrorDocuments and MIME-Types in the middle of the mod_rewrite code, or before or after. However, declaring them in the middle as you've done here does tend to break up the 'flow' of reading your rewriterules and may leave the later ones scrolled off-screen while you're tweaking the earlier ones (or vice-versa), leading to errors.]

I have moved my ErrorDocuments and MIME-Types to the top of .htaccess before 'RewriteEngine on'

[Your third rule could do with some efficiency improvements. All you need is:
[code]RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com
RewriteRule ^shop/buy-apparel-clothing-shirts\.html$ [mywebsite.com...] [R=301,L]
[code]

It is not at all clear why you wish to redirect to this "/shop" URL-path and 'expose' this "/shop" part of the path as a URL. It is also ill-advised to invoke multiple redirects on the same request. That is, if you truly have a good reason to do this redirect to "/shop", then your domain canonicalization rule should also redirect to "/shop" so that clients won't encounter two consecutive redirects if they request a non-canonical domain.]

These URLs are virtual which i use them on Google Ads. the url plays a huge role in keywords on Google Ads. When someone clicks on the Ads, they will be directed once to /shop/ I have since combined the 5 urls using or(¦) using your codes above. the original 'in-efficient' code was done from GUI via Cpanel>Redirects from my web hosting company.

[There are several additional 'lessons' in this one tweaked rule as compared to the original:
1) Use the power of regular expressions to write more-compact code.
2) Escape literal periods in regex patterns.
3) Do not end-anchor hostnames unless your pattern can account for FQDN-formatted hostnames -- and with or without a port number appended. For example, this is a perfectly-valid hostname which your original rule would fail to handle properly: www.example.com.:80
4) There is no need to quote or escape substitution URLs in the RewriteRule. They are literal strings except for back-reference tokens $1-$9 and %1-%9. The only characters that need to be escaped are therefore literal "$" and "%" characters, and the use of these literal characters is thankfully quite rare (and ill-advised).]

Give me some time to digest this section as i am pretty new to mod rewrite ;p

[It is a mistake to allow more than one URL to directly-access the same content. Your rules for mens' and womens' t-shirt shopping allow eight different URLs to 'compete' with each other for ranking on the exact same content. It's not likely that you'd enjoy having an additional eight competitors for your business, so why compete with yourself? Pick one single case-variation of your URLs, link only to that URL-type, and redirect all other case variations of that URL to the correctly-cased canonical URL.]

I am not sure what you mean by 'allow more than one URL to directly-access the same content' but here is my explanation.
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=13 [L] --> display mens t-shirt only

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=15 [L] --> display womens t-shirt only

I do admit a product may appear more than once eg. under mens AND under mens>tshirts AND under mens>tshirts>brand but i guess this is how my shopping cart filtering system works. Therefore content(product) might be the same for different URLs but the numbers of products displayed are different.

[The situation with "TwoPlay" is even worse as it accepts a potentially-unlimited number of arbitrarily-cased letters in the third URL-path part. Further, this part of the URL-path is not passed to your script for validation. As a result, an inspired competitor could kindly buy some links for you and point them to your site -- Links such as example.com/shop/mens/very-shoddy-quality-shoes/18/p2, and cause your site to become associated with those terms in search...]

I totally agree with you. i being trying to enforce a new rule using OR operand WITHOUT success. Can you spot any mistakes?

--NEW--
RewriteRule ^[Ss]hop/[Mm]ens¦[Ww]omens/[Tt]-shirts¦[Pp]olo¦[Ff]ootwear¦[Tt]ops¦[Bb]ags¦[Ss]horts/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

--OLD--
RewriteRule ^[Ss]hop/[Mm]ens¦[Ww]omens/[a-zA-z]+/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

===========================================================

#RemoveHandler .html .htm
#AddType application/x-httpd-php .php .htm .html
# AddHandler application/x-httpd-php5 .php
AddHandler application/x-httpd-php5 .html
AddHandler application/x-httpd-php5 .htm

ErrorDocument 404 /shop/error.php
ErrorDocument 401 /shop/401.php

RewriteEngine on
AuthUserFile "/home/lleitmot/.htpasswds/public_html/passwd"

#List of 5 redirects for Google Adwords
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com
RewriteRule ^shop/buy-apparel-clothing-shirts\.html$¦^shop/new-wholesale-accessories-clothes\.html$¦^shop/free-posted-shipping-services\.html¦^shop/singapore-online-shopping-business\.html¦^shop/women-fashion-dresses-bags\.html$ [mywebsite.com...] [R=301,L]

#Add a trailing slash to requested URLs
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ [mywebsite.com...] [L,R=301]

#Redirect domain.com to www.domain.com:
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ [mywebsite.com...] [L,R=301]

### By Product URL

#Houston - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/[Tt]-shirts/44/1187/
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Pp]olo/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]ops/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Pp]olo/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ff]ootwear/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Tt]ops/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Bb]ags/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ss]horts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]

### 3rd level - Inbox ¦ Encode ¦ TwoPlay

#Inbox - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/[Tt]-shirts/44/
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Pp]olo/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]ops/([0-9]+)/$ /shop/index.php?c=$1 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Pp]olo/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ff]ootwear/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Tt]ops/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Bb]ags/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ss]horts/([0-9]+)/$ /shop/index.php?c=$1 [L]

### 2nd level - T-shirts ¦ Tops ¦ Polo

#[Tt]-shirts - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/[Tt]-shirts/
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/$ /shop/index.php?c=13 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Pp]olo/$ /shop/index.php?c=14 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]ops/$ /shop/index.php?c=11 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/$ /shop/index.php?c=15 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Pp]olo/$ /shop/index.php?c=16 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ff]ootwear/$ /shop/index.php?c=17 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Tt]ops/$ /shop/index.php?c=12 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Bb]ags/$ /shop/index.php?c=37 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ss]horts/$ /shop/index.php?c=39 [L]

### 1st level - FOR HE ¦ FOR SHE

#FOR HE - [mywebsite.com...]
#http://mywebsite.com/shop/Mens/
RewriteRule ^[Ss]hop/[Mm]ens/$ /shop/index.php?c=2 [L]

RewriteRule ^[Ss]hop/[Ww]omens/$ /shop/index.php?c=3 [L]

### Paging ###

### 1st level - FOR HE ¦ FOR SHE

RewriteRule ^[Ss]hop/[Mm]ens/p([0-9]+)/$ /shop/index.php?page=$1&c=2 [L]

RewriteRule ^[Ss]hop/[Ww]omens/p([0-9]+)/$ /shop/index.php?page=$1&c=3 [L]

### 2nd level - T-shirts ¦ Tops ¦ Polo

RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=13 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Pp]olo/p([0-9]+)/$ /shop/index.php?page=$1&c=14 [L]
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]ops/p([0-9]+)/$ /shop/index.php?page=$1&c=11 [L]

RewriteRule ^[Ss]hop/[Ww]omens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=15 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Pp]olo/p([0-9]+)/$ /shop/index.php?page=$1&c=16 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ff]ootwear/p([0-9]+)/$ /shop/index.php?page=$1&c=17 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Tt]ops/p([0-9]+)/$ /shop/index.php?page=$1&c=12 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Bb]ags/p([0-9]+)/$ /shop/index.php?page=$1&c=37 [L]
RewriteRule ^[Ss]hop/[Ww]omens/[Ss]horts/p([0-9]+)/$ /shop/index.php?page=$1&c=39 [L]

### 3rd level - Inbox ¦ Encode ¦ TwoPlay

# [mywebsite.com...]
#RewriteRule ^[Ss]hop/[Mm]ens¦[Ww]omens/[Tt]-shirts¦[Pp]olo¦[Ff]ootwear¦[Tt]ops¦[Bb]ags¦[Ss]horts/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]
RewriteRule ^[Ss]hop/[Mm]ens¦[Ww]omens/[a-zA-z]+/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

### NEW ARRIVAL

RewriteRule ^[Ss]hop/p([0-9]+)/$ /shop/index.php?page=$1&c=0 [L]

Buster13

5:52 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



can someone check this part, eg. any syntax error? i cant seem to get it to work with the OR operand.

### 3rd level - Inbox ¦ Encode ¦ TwoPlay
# [mywebsite.com...]

RewriteRule ^[Ss]hop/[Mm]ens¦[Ww]omens/[Tt]-shirts¦[Pp]olo¦[Ff]ootwear¦[Tt]ops¦[Bb]ags¦[Ss]horts/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

g1smd

11:55 pm on Nov 25, 2009 (gmt 0)

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



I am not sure what you mean by 'allow more than one URL to directly-access the same content' but here is my explanation.
RewriteRule ^[Ss]hop/[Mm]ens/[Tt]-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=13 [L] --> display mens t-shirt only

Simply this:

example.com/[b]S[/b]hop/[b]M[/b]ens/[b]t[/b]-shirts/p23423456/

example.com/[b]s[/b]hop/[b]M[/b]ens/[b]T[/b]-shirts/p23423456/

example.com/[b]S[/b]hop/[b]m[/b]ens/[b]t[/b]-shirts/p23423456/

example.com/[b]s[/b]hop/[b]m[/b]ens/[b]T[/b]-shirts/p23423456/

is four different Duplicate Content URLs for the same content.

Pick ONE URL version as the canonical URL for that content, and 301 redirect all other requests to that canonical URL.

Buster13

9:03 am on Nov 26, 2009 (gmt 0)

10+ Year Member



the CAPS
/Shop /Mens /T-shirts /Womens etc
is not present/un-use in my website at all. In another word, my sitemap.xml only contains small letters.

I used deploy in htaccess for Google Adwords because CAPS can be useful in sense of visibility.

Therefore, do i still need to do 301 Redirect like what you say?

jdMorgan

3:55 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, because the link in your Adwords creates a duplicate URL which resolves to the same content as the non-Caps URL. You are essentially defining up to three additional URLs which will compete with your 'real' URL for ranking!

You'd be far better off to redirect these capitalized URLs to all-lowercase URLs, or alternatively, link only to these capitalized URLs on your own site as well, and then rewrite only those to your script. Even if you use this latter method, you will still need to redirect any non-capitalized requests to the capitalized versions in order to prevent the duplicate-content problem.

We seem to be in the position of having to twist your arm on this... I'll just point out that the most-memorable thread title on this subject posted in the Google forum at WebmasterWorld was "Duplicate Content - Get it Right or Perish." Nuff said...

---

As to your question above, you need to parenthesize each alternate 'thing' in the URL-path pattern:


RewriteRule ^[Ss]hop/[b]([[/b]Mm]ens¦[Ww]omen[b]s)[/b]/[b]([[/b]Tt]-shirts¦[Pp]olo¦[Ff]ootwear¦[Tt]ops¦[Bb]ags¦[Ss]hort[b]s)[/b]/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=[b]$4[/b]&c=[b]$3[/b] [L]

That is, you want "Shop", followed by ("mens" OR "womens"), followed by ("t-shirts" OR "tops" OR "shorts"), followed by "p<numbers>/". Without the parentheses around each group of alternate strings, you won't get the desired matches.

Jim

Buster13

8:04 am on Nov 30, 2009 (gmt 0)

10+ Year Member



the above solution doesn't work.

RewriteRule ^[Ss]hop/([Mm]ens¦[Ww]omens)/([Tt]-shirts¦[Pp]olo¦[Ff]ootwear¦[Tt]ops¦[Bb]ags¦[Ss]horts)/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

[mywebsite.net...]

Resulting URL:
[mywebsite.net...]

---
i notice that the $1 or $2 or $3 etc... does not work properly. no matter how i change above $X, the resulting URL always the same.

jdMorgan

2:48 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code with $4 and $3 is correct, as the $-numbers in the substitution refer to the sequentially-numbered parenthesized groups in the pattern: In the rule I posted, $4 is the page, and $3 is the 'item number'.

Be sure you replaced all broken pipe "¦" characters in that code with solid pipes before use; Posting on this forum modifies the pipe characters.

Also be sure to completely flush (delete) your browser cache after changing any server-side code and before testing; Otherwise, you will likely see 'stale' results cached by your browser.

Jim

Buster13

3:21 pm on Nov 30, 2009 (gmt 0)

10+ Year Member



Now, it works like a charm! I guess it might be because i never refresh my browser or for some unknown reasons it just wouldn't work earlier on.

I have learn about parentless bracket associated with $X AND re-arrange with most stringent rule on top of my htaccess AND had since removed all my [Ss] [Mm] [Ww]... to prevent duplicate content as per your advice.

My htaccess looks so much tweaked now.
You made my day! Cheers ;p

Buster13

4:43 am on Dec 6, 2009 (gmt 0)

10+ Year Member



My final copy of .htaccess with the help of Jim & g1smd. Feel free to comment, suggest and criticize if you like. Thanks! ;p
---

ErrorDocument 404 /shop/404.php
ErrorDocument 401 /shop/401.php

RewriteEngine on
AuthUserFile "/home/lleitmot/.htpasswds/public_html/passwd"

## make sure all under /shop/ use index.php (dynamic -> static use redirect.php)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /shop/index.php\?([^&]*&)*(c=¦p=)[^\ ]*\ HTTP/
RewriteRule ^shop/index\.php$ /redirect.php [L]

#List of 5 redirects for Google Adwords
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net
RewriteRule ^shop/(buy-apparel-clothing-shirts¦new-wholesale-accessories-clothes¦free-posted-shipping-services¦singapore-online-shopping-business¦women-fashion-dresses-bags)/$ http://www.example.net/shop/ [R=301,L]

#Add a trailing slash to requested URLs
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.net/$1/ [L,R=301]

#Redirect domain.com to www.domain.com:
RewriteCond %{REQUEST_URI} !redirect.php
RewriteCond %{HTTP_HOST} ^example.net [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [L,R=301]

### Paging /p1 /p2 /p3 ###

## 3rd level - Inbox ¦ Encode ¦ TwoPlay ##
# http://example.net/shop/womens/footwear/18/p2/
RewriteRule ^shop/(mens¦womens)/(t-shirts¦polo¦footwear¦tops¦bags¦shorts)/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$4&c=$3 [L]
#RewriteRule ^shop/mens¦womens/[a-zA-z]+/([0-9]+)/p([0-9]+)/$ /shop/index.php?page=$2&c=$1 [L]

## 2nd level - T-shirts ¦ Tops ¦ Polo ##
RewriteRule ^shop/mens/t-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=13 [L]
RewriteRule ^shop/mens/polo/p([0-9]+)/$ /shop/index.php?page=$1&c=14 [L]

RewriteRule ^shop/womens/t-shirts/p([0-9]+)/$ /shop/index.php?page=$1&c=15 [L]
RewriteRule ^shop/womens/polo/p([0-9]+)/$ /shop/index.php?page=$1&c=16 [L]

## 1st level - FOR HE ¦ FOR SHE ##
RewriteRule ^shop/mens/p([0-9]+)/$ /shop/index.php?page=$1&c=2 [L]

RewriteRule ^shop/womens/p([0-9]+)/$ /shop/index.php?page=$1&c=3 [L]

## NEW ARRIVAL ##
RewriteRule ^shop/p([0-9]+)/$ /shop/index.php?page=$1&c=0 [L]

### /shop/ ###

## By Product URL ##
# Houston - http://example.net/shop/index.php?c=44&p=1187
# http://example.net/shop/Mens/t-shirts/44/1187/
RewriteRule ^shop/mens/t-shirts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^shop/mens/polo/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]

RewriteRule ^shop/womens/t-shirts/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]
RewriteRule ^shop/womens/polo/([0-9]+)/([0-9]+)/$ /shop/index.php?c=$1&p=$2 [L]

## 3rd level - Inbox ¦ Encode ¦ TwoPlay ##
# Inbox - http://example.net/shop/index.php?c=44
# http://example.net/shop/Mens/t-shirts/44/
RewriteRule ^shop/mens/t-shirts/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^shop/mens/polo/([0-9]+)/$ /shop/index.php?c=$1 [L]

RewriteRule ^shop/womens/t-shirts/([0-9]+)/$ /shop/index.php?c=$1 [L]
RewriteRule ^shop/womens/polo/([0-9]+)/$ /shop/index.php?c=$1 [L]

## 2nd level - T-shirts ¦ Tops ¦ Polo ##
# t-shirts - http://example.net/shop/index.php?c=13
# http://example.net/shop/Mens/t-shirts/
RewriteRule ^shop/mens/t-shirts/$ /shop/index.php?c=13 [L]
RewriteRule ^shop/mens/polo/$ /shop/index.php?c=14 [L]

RewriteRule ^shop/womens/t-shirts/$ /shop/index.php?c=15 [L]
RewriteRule ^shop/womens/polo/$ /shop/index.php?c=16 [L]

## 1st level - FOR HE ¦ FOR SHE ##
# FOR HE - http://example.net/shop/index.php?c=2
# http://example.net/shop/Mens/
RewriteRule ^shop/mens/$ /shop/index.php?c=2 [L]

RewriteRule ^shop/womens/$ /shop/index.php?c=3 [L]

[edited by: jdMorgan at 4:21 pm (utc) on Dec. 6, 2009]
[edit reason] Disabled smilies in code. [/edit]

jdMorgan

4:39 pm on Dec 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See bolded changes:

## make sure all under /shop/ use index.php (dynamic -> static use redirect.php)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /shop/inde[b]x\.p[/b]hp\?([^&]*&)*[b][cp]=[/b][^\ ]*\ HTTP/
RewriteRule ^shop/index\.php$ /redirect.php [L]
#
#List of 5 redirects for Google Adwords
# [i]The first RewriteCond here may not be needed at all, unless you expect to get requests for other hostnames
# such as example.[b]com[/b] or [b]subdomain[/b].example.net which you *do not* want to redirect.[/i]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net [b][NC][/b]
RewriteRule ^shop/(buy-apparel-clothing-shirts¦new-wholesale-accessories-clothes¦free-posted-shipping-services¦singapore-online-shopping-business¦women-fashion-dresses-bags)/$ http://www.example.net/shop/ [R=301,L]
#
#Add a trailing slash to requested URLs
# [i](Always do 'file exists' and 'directory exists' checks [b]last[/b] whenever possible. They are VERY slow)[/i]
# RewriteCond %{REQUEST_URI} !exampl[b]e\.p[/b]hp
RewriteCond %{REQUEST_URI} [b]!/$[/b]
[b]RewriteCond %{REQUEST_FILENAME} !-f [/b]
RewriteRule ^(.*)$ http://www.example.net/$1/ [L,R=301]
#
#Redirect domain.com to www.domain.com:
RewriteCond %{REQUEST_URI} !redirec[b]t\.p[/b]hp
RewriteCond %{HTTP_HOST} ^exampl[b]e\.n[/b]et [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [L,R=301]

These are tweaks for efficiency and robustness only. Similar changes may be required in the other rules, but I'm only intending to provide examples here.

Jim

Buster13

7:21 am on Dec 7, 2009 (gmt 0)

10+ Year Member



(Always do 'file exists' and 'directory exists' checks last whenever possible. They are VERY slow)

Do you mean I should move the whole section for Adding / to bottom of my .htaccess OR just rearrange the REQUEST_URL / REQUEST_FILENAME order?

Thanks in advance!

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



Put any RewriteConds of the form

RewriteCond %{REQUEST_FILENAME} -<flag>
-or-
RewriteCond %{REMOTE_HOST} <pattern>

last of all the RewriteConds in any given rule whenever possible.

File-exists and directory-exists checks invoke calls to the operating system. These may result in actual dosk read accesses, which are thousands of times slower than just executing code. Remote hostname lookups invoke DNS lookup requests -- Your server must contact a DNS server and wait for a DNS "whois" response. Again, this can be thousands of times slower than just running your .htaccess code.

Therefore, these two kinds of RewriteConds should be placed last in the 'stack' of RewriteConds for any given RewriteRule, and unnecessary execution of these functions should always be avoided by making the RewriteRule pattern and the preceding RewriteConds as specific as possible; RewriteConds are not processed at all unless the requested URL-path matches the RewriteRule's pattern, and later RewriteConds are not processed unless previous RewriteConds return 'true' -- See Apache mod_rewrite documentation for details.

Jim

Buster13

7:17 am on Dec 21, 2009 (gmt 0)

10+ Year Member



mine is last and just before the RewriteRule as per above posts.
therefore i am doing right, as per your advice?

g1smd

8:46 am on Dec 21, 2009 (gmt 0)

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



Yes, the RewriteCond 'file exists' check in any given rule, should be the last one before the RewriteRule line itself.

jdMorgan

5:03 pm on Dec 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry if my comment was confusing. I was just pointing out the reason for the major change between your original code:

#Add a trailing slash to requested URLs
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.net/$1/ [L,R=301]

and the revised code that I posted:

#Add a trailing slash to requested URLs
# [i](Always do 'file exists' and 'directory exists' checks [b]last[/b] whenever possible. They are VERY slow)[/i]
# RewriteCond %{REQUEST_URI} !exampl[b]e\.p[/b]hp
RewriteCond %{REQUEST_URI} [b]!/$[/b]
[b]RewriteCond %{REQUEST_FILENAME} !-f [/b]
RewriteRule ^(.*)$ http://www.example.net/$1/ [L,R=301]

Your original code calls the operating system to go check the disk for every single request that makes it this far in the mod_rewrite processing. My revised code checks the disk only if the requested URL-path does not end with a slash.

If possible -given your site's URL- and filesystem layout and naming conventions- it would be good to add more qualifications (more RewriteConds) to further reduce the number of unnecessary disk checks. For example, you probably don't want or need to add a slash if the requested path is "robots.txt", "sitemap.xml", "favicon.ico", or any of your various .php and image or media files. It would be faster to 'manually' check for these excluded paths in the code than it would be to check the disk unnecessarily.

Jim