Forum Moderators: phranque

Message Too Old, No Replies

URL Rewrite *i really tried and i need help

         

Buster13

7:24 am on Nov 5, 2009 (gmt 0)

10+ Year Member



my website eg. example.com
c = category ID
p = product ID

http://example.com/shop/index.php?c=13&p=1053

i want to rewrite to

http://example.com/shop/mens-tops/1053
will hard-code "mens-tops" as "c=13"

i tried:

RewriteRule
^shop/mens-tops/([0-9]+)$
shop/index.php?c=13&p=$1

doesnt work above

TheMadScientist

7:31 am on Nov 5, 2009 (gmt 0)

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



Hi Buster13,

Welcome to WebmasterWorld!

If you have the line breaks in your file, it won't work...

# Make sure have the correct options setting:
# If you get a server error, comment out (# = Comment) or delete the following.
Options +FollowSymLinks

# Make sure you turn the engine on:
RewriteEngine on

# Make sure the rule is on one line, and you use the [L] flag, unless you know you don't need it:
ReweriteRule ^shop/mens-tops/([0-9]+)$ /shop/index.phpc=13&p=$1 [L]

# Make sure you have a look in the Library [webmasterworld.com] for more information than I can give you in a single post.

Buster13

7:48 am on Nov 5, 2009 (gmt 0)

10+ Year Member



tried

- no break
- with/without Options +FollowSymLinks
- RewriteEngine on

but still fails

here my finding, if i use URL(it works!)

URL: http://example.com/shop/mens-tops-1053.html

RewriteRule shop/mens-tops-(.*)\.html$ shop/index.php?c=11&p=$1

------
but i need,
URL: http://example.com/shop/mens-tops/1053

the difference is only after tops- VS tops/

TheMadScientist

8:14 am on Nov 5, 2009 (gmt 0)

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



Okay, jdMorgan's probably going to yell at me for this, because it's non-standard, but I ran into a situation the other day where I had to unanchor the left side of my rule to get a match.

I honestly don't have any idea why and maybe he will, but I haven't looked into it enough and don't, because I just needed the flipping rule to work... I used exactly the same pattern, but removed the anchors and it matched, and the reason I'm suggesting it is it's only happening with my extensionless URLs on the site and yours happen to be entensionless... so try:

# Try this 1st:
RewriteRule ^shop/mens-tops/([0-9]+) /shop/index.php?c=11&p=$1 [L]

# If that doesn't work, try:
RewriteRule shop/mens-tops/([0-9]+) /shop/index.php?c=11&p=$1 [L]

# Make sure you use the [L] Flag (last) on the right side of your rules unless you know you don't need it & I always recommend using the preceding / on the right side of your rules so it's a server relative URL, because it generally prevents extra, unnecessary headaches from an already difficult module.

# The biggest problem with removing the anchors is sometimes you match too much which is not good, so you have to make sure you have rules in the correct order and eliminate URLs which should not qualify for a rule, but do since there is no anchor from being processed ASAP.

TheMadScientist

8:25 am on Nov 5, 2009 (gmt 0)

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



If you get either of those to match, you will need to add some exclusions to the rule in a condition, because as they are written they will match and rewrite:

URL: http://example.com/shop/mens-tops/1053.html
URL: http://example.com/shop/mens-tops/1053.php
URL: http://example.com/shop/mens-tops/1053anything-else-here

Which could easily duplicate your content.
The follwing might not be exact, and you'll need to test, but this should be close:

RewriteCond %{THE_REQUEST} ^[A-Z]{2,6}\ /[^/]{4}/[^/]{9}/$1\ HTTP/1
RewriteRule ^shop/mens-tops/([0-9]+) /shop/index.php?c=11&p=$1 [L]

# I think the end anchor was the issue, but haven't looked at the site in a few days and don't remember off the top of my head, so I used the one with the start anchor and no end-anchor as an example in this ruleset.

Buster13

8:43 am on Nov 5, 2009 (gmt 0)

10+ Year Member



===== PASS =========
http://example.com/shop/mens-tops-1053.html

RewriteRule ^shop/mens-tops-(.*)\.html$ shop/index.php?c=11&p=$1 [L]

===== PASS =========
http://example.com/shop/mens-tops-1053

RewriteRule ^shop/mens-tops-(.*)$ shop/index.php?c=11&p=$1 [L]

======= FAIL (partial) =======
http://example.com/shop/mens-tops/1053
RewriteRule ^shop/mens-tops/(.*)$ shop/index.php?c=11&p=$1 [L]

ALL my CSS rules are missing.

Buster13

8:53 am on Nov 5, 2009 (gmt 0)

10+ Year Member



something is FISHY here.. found this in my config.php

// setting up the web root and server root for
// this shopping cart application
$thisFile = str_replace('\\', '/', __FILE__);
$docRoot = $_SERVER['DOCUMENT_ROOT'];

$webRoot = str_replace(array($docRoot, 'library/config.php'), '', $thisFile);
$srvRoot = str_replace('library/config.php', '', $thisFile);

define('WEB_ROOT', $webRoot);
define('SRV_ROOT', $srvRoot);
============================================
alot of my website PHP files (top) has,

<?php
if (!defined('WEB_ROOT')) {
exit;
}
?>
===========================================
i have a feeling 'exit' caused my CSS to break. just my 2 cents
===========================================
my directory is as follow,

/public_html/shop
/public_html/shop/library
/public_html/shop/library/config.php

TheMadScientist

9:14 am on Nov 5, 2009 (gmt 0)

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



ALL my CSS rules are missing.

Check your URLs...
My guess is their directory relative, rather than absolute or server relative.

IOW:
I'm guessing they're this:
path/to-the-css.css

Not:
/path/to-the-css.css
OR
http://www.example.com/path/to-the-css.css

===== PASS =========
http://example.com/shop/mens-tops-1053.html

RewriteRule ^shop/mens-tops-(.*)\.html$ shop/index.php?c=11&p=$1 [L]

===== PASS =========
http://example.com/shop/mens-tops-1053

RewriteRule ^shop/mens-tops-(.*)$ shop/index.php?c=11&p=$1 [L]

======= FAIL (partial) =======
http://example.com/shop/mens-tops/1053
RewriteRule ^shop/mens-tops/(.*)$ shop/index.php?c=11&p=$1 [L]

You're could run into the same issue above as I noted with removing the end anchor by using (.*), but in thinking about it a bit more, as long as you serve a proper 404 within your PHP by checking for a match of the passed variable and serving a 404 if there is no match, you should be able to get away with a 'less precise' match... It's something to 'triple check' twice on 3 different days though, because if you don't serve a 404 from the PHP when the variable is not an exact match, but rather deliver the content you could triplicate your content and not even know you did it.

Also, if you are going to match to 'match everything' I would recommend using ([^.]+) rather than (.*) if there is anything after the (.*) in the pattern to be matched, so you prevent recursion.

([^.]+) = 'not a dot' one or more times, so this:
RewriteRule ^shop/mens-tops-(.*)\.html$ shop/index.php?c=11&p=$1 [L]

Would become:
RewriteRule ^shop/mens-tops-([^.]+)\.html$ shop/index.php?c=11&p=$1 [L]

Buster13

9:29 am on Nov 5, 2009 (gmt 0)

10+ Year Member



you got it ! +1

my html header
<link href="../css/basic.css" rel="stylesheet" type="text/css" />

if i modified to add '../'

<link href="../../css/basic.css" rel="stylesheet" type="text/css" />

some of my CSS styles returned to me.
now all i need is to 'proper' adjust the path...

thanks you so much!

Buster13

9:37 am on Nov 5, 2009 (gmt 0)

10+ Year Member



RewriteRule ^shop/mens-tops/(.*)$ shop/index.php?c=11&p=$1 [L]

is there anyway to make about rewrite rule to think that

"mens-tops/" is a SINGLE word (inclusive of char /)

TheMadScientist

9:49 am on Nov 5, 2009 (gmt 0)

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



This will work in any directory, and you won't ever have to count dots again:
<link href="/css/basic.css" rel="stylesheet" type="text/css" />

Not sure what you mean in your second question?

If you are trying to reference it in the rewrite you would just surround the portion you want to 'back-reference' in parenthesis:

RewriteRule ^shop/(mens-tops/)(.*)$ shop/index.php?c=$1&p=$2 [L]

$1 = mens-tops/
$2 = the numbers stored previously as $1.
* Always count the ( to know which number to use...

Buster13

9:52 am on Nov 6, 2009 (gmt 0)

10+ Year Member



Thank you for your help!
I got the URL Rewrite stuffs working.
Below is what i have at the moment, tested and working fine.

By the way, can i have someone review below codes that i have written? eg. if i missed some security pt, some symbols or better way of implementing.

# BY Sex (where mens_id = 2)
#(bef) [mysite.com...]
#(aft) [mysite.com...]

RewriteRule ^[S-Ss-s]hop/[M-Mm-m]ens/$ shop/index.php?c=2 [L]

-------
# BY Sex > ClothType (where tshirt_id = 13)
#(bef) [mysite.com...]
#(aft) [mysite.com...]

RewriteRule ^[S-Ss-s]hop/[M-Mm-m]ens/[T-Tt-t]-shirts/$ shop/index.php?c=13 [L]

-------
# BY Sex > ClothType > Brand
#(bef) [mysite.com...]
#(aft) [mysite.com...]

RewriteRule ^[S-Ss-s]hop/[M-Mm-m]ens/[T-Tt-t]-shirts/(.*)/$ shop/index.php?c=$1 [L]

-------
# BY Sex > ClothType > Brand > Product
#(bef) [mysite.com...]
#(aft) [mysite.com...]

RewriteRule ^[S-Ss-s]hop/[M-Mm-m]ens/[T-Tt-t]-shirts/(.*)/(.*)/$ shop/index.php?c=$1&p=$2 [L]

g1smd

1:02 pm on Nov 6, 2009 (gmt 0)

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



Please re-read the previous warnings about gratuitously using (.*) patterns.

If there are ANY characters to be matched after the pattern, use a more efficient pattern.

Never use (.*) twice (or more) within a single pattern. Try to avoid using it at all.

jdMorgan

1:41 pm on Nov 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace "[S-Ss-s]" with "[Ss]", "/(.*)/" with "/([^/]+)/", and "/(.*)/(.*)/" with "/([^/]+)/([^/]+)/" for *much* better performance.

Make a similar correction for "[M-Mm-m]" and "[T-Tt-t]".

Better yet, add external redirect rules to redirect mis-cased URLs to correct-case URLs before invoking your internal rewrites. Then make sure that the links on your site refer only to the correctly-cased URLs. This will avoid duplicate content problems with search engines...

Jim