First try on Mod Rewrite

New to mod_rewrite? This is a good place to start.

First try on Mod Rewrite

Postby wxwx » Tue May 12, 2009 4:51 pm

Code: Select all
RewriteEngine On
RewriteBase /product/


1. Files : index.php contact.php
Code: Select all
- Want change it to be  domain.com/product/
- Want change it to be  domain.com/product/contact/


2. Files : forgot.php?todo=success forgot.php?todo=invalid
Code: Select all
- Want change to be domain.com/product/forgot/success/
- Want change to be domain.com/product/forgot/invalid/


3. File : details.php?pid=38
Code: Select all
- Want change to be domain.com/product/details/38/


4. File : category.php?pid=80&cname=product20%name
Code: Select all
- Want change to be domain.com/product/category/product-name/


5. File :category.php?pid=84&cname=product%20name&scatname=product%20name
Code: Select all
- Want change to be domain.com/product/category/product-name/product-name/

Code: Select all
- Want remove php extension without a write domain name to .htaccess
- Want redirect URL without slash to /
- Example domain.com/product/contact to domain.com/product/contact/



Let me know how to do this.. any inputs I really appreciate
wxwx
 
Posts: 8
Joined: Tue May 12, 2009 4:42 pm

Postby richardk » Wed May 13, 2009 7:57 am

In a .htaccess file in /product
Code: Select all
Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On

RewriteBase /product/

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ ./$1/ [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^contact/?$ ./contact.php [QSA,L]
RewriteRule ^forgot/(invalid|success)/?$ ./forgot.php?todo=$1 [QSA,L]
RewriteRule ^details/([0-9]+)/?$ ./details.php?pid=$1 [QSA,L]


4. File : category.php?pid=80&cname=product20%name
Code: Select all
- Want change to be domain.com/product/category/product-name/


5. File :category.php?pid=84&cname=product%20name&scatname=product%20name
Code: Select all
- Want change to be domain.com/product/category/product-name/product-name/

Please provide an example of each.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby wxwx » Wed May 13, 2009 6:05 pm

thanks richardk.. working great!

Code: Select all
RewriteRule ^contact/?$ ./contact.php [QSA,L]


1. I found another problem :( which i need to add all file .php to htaccess.
there has a simple way to make my all my .php will convert automated.

2. I tried to add
Code: Select all
RewriteRule ^(.*)/$ $1.php [L]

but will be infinity loop.

3. when i type domain.com/product/index.php will go to domain.com/product/index/


4. category.php?pid=80&cname=product20%name here is my rewrite

Code: Select all
RewriteRule ^category/([^/]*)/([^/]*)/$ category.php?pid=$1&cname=$2 [L]
RewriteRule ^category/([^/]*)/([^/]*)/([^/]*)/$ category.php?pid=$1&cname=$2&scatname=$3 [L]
RewriteRule ^category/([^/]*)/$ category.php?pid=$1 [L]


can combine it? and i want the url without slash will ending with /

5. actually my .htaccess too long.. because using online generator.. problem me to understand :)

6. this is for paging page.. need to write in single line if can be done and will ending with slash / too.


Code: Select all
RewriteRule ^page/([^/]*)/$ index.php?p=$1 [L]
RewriteRule ^list-view/page/([^/]*)/$ list-view.php?p=$1 [L]
RewriteRule ^history/page/([^/]*)/$ history.php?p=$1 [L]
RewriteRule ^pending-orders/page/([^/]*)/$ pending-orders.php?p=$1 [L]
RewriteRule ^completed-orders/page/([^/]*)/$ completed-orders.php?p=$1 [L]
wxwx
 
Posts: 8
Joined: Tue May 12, 2009 4:42 pm

Postby richardk » Fri May 15, 2009 8:30 am

Try
Code: Select all
Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On

RewriteBase /product/

# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(html?|php)$ ./$1 [R=301,L]

# Remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCIRPT_FILENAME} -f
RewriteRule ^(.+)\.php$ ./$1/ [R=301,L]

# Force trailing slashes.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# /file/ to /file.php
RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^([^/]+)/?$ ./$1.php [QSA,L]

RewriteRule ^forgot/(invalid|success)/?$ ./forgot.php?todo=$1 [QSA,L]
RewriteRule ^details/([0-9]+)/?$ ./details.php?pid=$1 [QSA,L]
RewriteRule ^category/([0-9]+)(?:/([^/]+)(?:/([^/]+))?)?/$ ./category.php?pid=$1&cname=$2&scatname=$3 [QSAL]
RewriteRule ^page/([0-9]+)/$ index.php?p=$1 [L]
RewriteRule ^(completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby wxwx » Fri May 15, 2009 8:47 am

I test on local it's working perfect.. but when I upload the htaccess to sever then Firefox said Redirect Loop..

I think this part has a something wrong

Code: Select all
# /file/ to /file.php
RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^([^/]+)/?$ ./$1.php [QSA,L]


let me know 8)
wxwx
 
Posts: 8
Joined: Tue May 12, 2009 4:42 pm

Postby wxwx » Sat May 16, 2009 9:02 am

not working
Code: Select all
RewriteRule ^page/([0-9]+)/$ index.php?p=$1 [L]


working :D
Code: Select all
RewriteRule ^(completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]


hep ya richardk :wink:
wxwx
 
Posts: 8
Joined: Tue May 12, 2009 4:42 pm

Postby richardk » Sat May 16, 2009 9:12 am

not working
Code: Select all
RewriteRule ^page/([0-9]+)/$ index.php?p=$1 [L]

What happens? What URL are you visiting?

Try
Code: Select all
Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On

RewriteBase /product/

# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(html?|php)$ ./$1 [R=301,L]

# Remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCIRPT_FILENAME} -f
RewriteRule ^(.+)\.php$ ./$1/ [R=301,L]

# Force trailing slashes.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# /file/ to /file.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^([^/]+)/?$ ./$1.php [QSA,L]

RewriteRule ^forgot/(invalid|success)/?$ ./forgot.php?todo=$1 [QSA,L]
RewriteRule ^details/([0-9]+)/?$ ./details.php?pid=$1 [QSA,L]
RewriteRule ^category/([0-9]+)(?:/([^/]+)(?:/([^/]+))?)?/$ ./category.php?pid=$1&cname=$2&scatname=$3 [QSAL]
RewriteRule ^page/([^/]+)/$ ./index.php?p=$1 [L]
RewriteRule ^(completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby wxwx » Sat May 16, 2009 11:42 am

My current .htaccess

Code: Select all
Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On

RewriteBase /product/

# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(html?|php)$ ./$1 [R=301,L]

# Remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCIRPT_FILENAME} -f
RewriteRule ^(.+)\.php$ ./$1/ [R=301,L]

# Force trailing slashes.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# /file/ to /file.php
RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^([^/]+)/?$ ./$1.php [QSA,L]

# reset part
RewriteRule ^forgot/(invalid|success)/?$ ./forgot.php?todo=$1 [QSA,L]

# product details
RewriteRule ^[^/]+/([0-9]+)/?$ ./details.php?pid=$1 [QSA,L]
##RewriteRule ^details/([0-9]+)/?$ ./details.php?pid=$1 [QSA,L] - This line changed. removed ^details and added slash / to separate product id.

# category part
RewriteRule ^category/([0-9]+)(?:/([^/]+)(?:/([^/]+))?)?/$ ./category.php?pid=$1&cname=$2&scatname=$3 [QSA,L]

# paging part
RewriteRule ^page/([^/]+)/$ ./index.php?p=$1 [L]
RewriteRule ^(grid|completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]



1. I test line by line.. and line # Remove .php will be the redirect loop. When I remove that lines all link working fine except the .php will not return to slash at the end.

Code: Select all
RewriteRule ^page/([^/]+)/$ ./index.php?p=$1 [L]


2. This line go to domain.com/product/page/1/ and will display contents for # product details.

But for
Code: Select all
RewriteRule ^(grid|completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]
working fine go to the next page and will not display the content for # product details

Let me know :wink:
wxwx
 
Posts: 8
Joined: Tue May 12, 2009 4:42 pm

Postby richardk » Sun May 17, 2009 9:11 am

1. I test line by line.. and line # Remove .php will be the redirect loop. When I remove that lines all link working fine except the .php will not return to slash at the end.

Code: Select all
SCIRPT

should be
Code: Select all
SCRIPT


1. I test line by line.. and line # Remove .php will be the redirect loop. When I remove that lines all link working fine except the .php will not return to slash at the end.

Code: Select all
RewriteRule ^page/([^/]+)/$ ./index.php?p=$1 [L]


2. This line go to domain.com/product/page/1/ and will display contents for # product details.

But for
Code: Select all
RewriteRule ^(grid|completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]

working fine go to the next page and will not display the content for # product details

Could you post/PM me the link so i can see it?
Is it actually a problem with the mod_rewrite or is it a problem with the script? (Does the right file get loaded? Is the p variable set correctly?)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Mon May 18, 2009 11:29 am

1. I test line by line.. and line # Remove .php will be the redirect loop. When I remove that lines all link working fine except the .php will not return to slash at the end.
Code: Select all
RewriteRule ^page/([^/]+)/$ ./index.php?p=$1 [L]

What happens if you replace [L] with [R,L]?

2. This line go to domain.com/product/page/1/ and will display contents for # product details.

But for
Code: Select all
RewriteRule ^(grid|completed-orders|history|list-view|pending-orders)/page/([0-9]+)/$ ./$1.php?p=$2 [QSA,L]

working fine go to the next page and will not display the content for # product details

The problem is with the script not the mod_rewrite. ./grid/page/2/ works.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 100 guests

cron