Hi All :) and i have a rewrite question, well i think it is

Discuss practical ways rearrange URLs using mod_rewrite.

Hi All :) and i have a rewrite question, well i think it is

Postby HyperVirus » Wed Jan 21, 2009 12:23 pm

Hi All

Nice to see there is a board just for mod_rewrite as its a pain in the bum :D

I have managed to figure out some stuff by myself and its working quite well:

RewriteRule ^about.html$ index.php?a
RewriteRule ^home.html$ index.php?h
RewriteRule ^shipping_returns.html$ index.php?sr
RewriteRule ^payment_types.html$ index.php?pt
RewriteRule ^customer_feedback.html$ index.php?cf
RewriteRule ^coming_soon.html$ index.php?cs
RewriteRule ^privacy_policy.html$ index.php?pp
RewriteRule ^contact_us.html$ index.php?cu

RewriteRule ^catagory/([0-9]+)/([^/]+)\.html$ index.php?c=$1
RewriteRule ^product/([0-9]+)/([0-9]+)/([^/]+)\.html$ index.php?i=$1&ic=$2

this does as it says redirects html files to various index with _GET's which is working well.

the bottom 2 are basicly to allow me to display a product name in the .html file as the filename.html is ignored the only varaibels are the product/{ITEM_NUMBER}/{ITEM_CATAGORY}/blahblah.html
but with what im explaining i want to do mysite.com/the_product.html and that take you to the right page. i understand this can be done but each product is dynamic and i dont want to type out the rules each time. also see below for more rambalings. :)

But i am thinking if i want admins of this site im not going to want them to load up the .htaccess file and add blocks of code in, as they wont be able to understand it, shucks i don't even understand it :P

so i was browsing one of my customers sites which i installed (CS-Cart) [im writing my own] and he has SEO what ever that is and mod_rewrite. his .htaccess file looks like this.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

but he has hundreds of products on his site and they all have there own links and stuff but with nothing in the .htaccess file, how is this possible?

i have found a list of link names in a MySQL table. but how is this done?
Is it easy to do, i tried to find every SEO / Mod_rewrite file in the cs-cart system and tried to look at the code but couldn't understand any of it. as it was properly organized and stuff.

i tend to write stuff as i see fit. im not the best for keeping up programming practices.

If anyone could help i would be most grateful, and will give you a vCookie :)

Thanks
Kev
HyperVirus
 
Posts: 3
Joined: Wed Jan 21, 2009 12:12 pm

Postby richardk » Wed Jan 21, 2009 1:56 pm

Your mod_rewrite should be
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^about\.html$             /index.php?a  [QSA,L]
RewriteRule ^home\.html$              /index.php?h  [QSA,L]
RewriteRule ^shipping_returns\.html$  /index.php?sr [QSA,L]
RewriteRule ^payment_types\.html$     /index.php?pt [QSA,L]
RewriteRule ^customer_feedback\.html$ /index.php?cf [QSA,L]
RewriteRule ^coming_soon\.html$       /index.php?cs [QSA,L]
RewriteRule ^privacy_policy\.html$    /index.php?pp [QSA,L]
RewriteRule ^contact_us\.html$        /index.php?cu [QSA,L]

RewriteRule ^catagory/([0-9]+)/[^/]+\.html$         /index.php?c=$1       [QSA,L]
RewriteRule ^product/([0-9]+)/([0-9]+)/[^/]+\.html$ /index.php?i=$1&ic=$2 [QSA,L]

The L flags being the most important change. They stop processing when the rule matches.

the bottom 2 are basicly to allow me to display a product name in the .html file as the filename.html is ignored the only varaibels are the product/{ITEM_NUMBER}/{ITEM_CATAGORY}/blahblah.html
but with what im explaining i want to do mysite.com/the_product.html and that take you to the right page. i understand this can be done but each product is dynamic and i dont want to type out the rules each time.

It takes more than just mod_rewrite. See FAQ: How to change a numeric ID into a name/title and post back here about anything you don't understand.

im writing my own

Then design it to use the product name (a slug) in the URL/_GET variable instead of a numeric ID.

so i was browsing one of my customers sites which i installed (CS-Cart) [im writing my own] and he has SEO what ever that is and mod_rewrite. his .htaccess file looks like this.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

but he has hundreds of products on his site and they all have there own links and stuff but with nothing in the .htaccess file, how is this possible?

The mod_rewrite just passes all 404 errors to the index.php file which handles it all (probably with the REQUEST_URI variable). You use more mod_rewrite to set a _GET variable instead of using REQUEST_URI.

have found a list of link names in a MySQL table. but how is this done?

When you create a new product the links are put in the table (usually an automatically sanitised version of the product name). Then when the PHP gets the product name from REQUEST_URI or _GET it searches the database for that product slug.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby HyperVirus » Wed Jan 21, 2009 2:50 pm

Well thats what ive sort of done. for product pages for example i have done

http://localhost:9876/Commerce_v2/produ ... tem_2.html

/product = random text (so google will find index it as a product)
/2 = the product id
/1 = catagory id
/This_is_test_item_2.html = isnt needed its just so google will index it properly

is this what you mean i should do? cos these items are fine (i think)

i was more on about the standard pages. like home

http://localhost:9876/Commerce_v2/home.html

the user will be adding there own pages so as far as i know they will have to rewrite them manually? or am i getting the complete wrong end of the stick? sorry im a bit think when it comes to this.

Thanks
Kev
HyperVirus
 
Posts: 3
Joined: Wed Jan 21, 2009 12:12 pm

Postby richardk » Wed Jan 21, 2009 3:58 pm

i was more on about the standard pages. like home

Oh. I thought you wanted to remove the product and category IDs.

the user will be adding there own pages so as far as i know they will have to rewrite them manually?

Then you would be better off having /home.html to /index.php?page=home because then you could write a general rule like
Code: Select all
# Make sure the HTML file doesn't exist. If you have
# no HTML files you can remove this.
RewriteCond %{SCRIPT_FILENAME} !-f
# Any.html file to /index.php?page=Any.
RewriteRule ^([^/]+)\.html$ /index.php?page=$1 [QSA,L]

Then you can use the page _GET variable, search to see if the page exists and either display it or a 404 error.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby HyperVirus » Wed Jan 21, 2009 4:03 pm

ah i see that makes more sense. i was using small ids like ?h for home as i didn't want the user to have a 40 page long URL, but guess with mod rewrite that wouldn't be a problem. ok i will try it this way.

If i could remove those ID's (for the product URL) and the /products/ bit so it was just mysite.com/i_am_a_item.html that would be great.

is that the same system as above? and searching a database or something for the filename then loading the correct page? i think i will be able to manage that i think.

Thanks for the heads up it was a big help :) (well if i understood it all correctly)
HyperVirus
 
Posts: 3
Joined: Wed Jan 21, 2009 12:12 pm

Postby richardk » Wed Jan 21, 2009 4:15 pm

If i could remove those ID's (for the product URL) and the /products/ bit so it was just mysite.com/i_am_a_item.html that would be great.

If you remove both the IDs and /products it will conflict with the /home.html rewrite. (Unless you want /i_am_a_item.html to go to /index.php?page=i_am_a_item as well.)

is that the same system as above? and searching a database or something for the filename then loading the correct page? i think i will be able to manage that i think.

Yes. For /products/product_name.html
Code: Select all
RewriteRule ^products/([^/]+)\.html$ /index.php?product=$1 [QSA,L]

When adding a product (eg. TV Remote) you would create a URL slug (eg. tv_remote) and add it into the products table in the database with the other details. When someone goes to /products/tv_remote.html mod_rewrite sends the request to /index.php?product=tv_remote. In your PHP script you would use $_GET['product'] search your products table to see if the slug exists. If it does, display the product, if it doesn't display an 404 page.

You could do the same for categories.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 19 guests

cron