.htaccess tutorial

htaccess Elite


RewriteRule / Regular expression problem

Ask your mod_rewrite and Redirection questions here, and get answers!

RewriteRule / Regular expression problem

Postby jiv » 10 May 2008 17:27

Hi All

I have a problem. Here's is my RewriteRule

Code: Select all
Options -Indexes

RewriteEngine On

RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]

RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]


This works when a user type http://www.mysite.com/products/ but not with http://www.mysite.com/products (without the trailing slash) ...

Can anyone help me?

Thanks a lot!
jiv
 
Posts: 3
Joined: 10 May 2008 17:20

Re: RewriteRule / Regular expression problem

Postby mod_rewrite » 11 May 2008 04:17

jiv wrote:This works when a user type /products/ but not with /products (without the trailing slash) ...

Can anyone help me?

Thanks a lot!


Code: Select all
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\/]+)\ HTTP/ [NC]
RewriteRule ^(.*)$ http://www.site.com/%1/ [R=301,L]


That should work great!
mod_rewrite
 
Posts: 102
Joined: 30 Oct 2006 19:55

Re: RewriteRule / Regular expression problem

Postby jiv » 12 May 2008 15:37

Thanks for the reply mod_rewrite

In fact, I have originally 5 variable lines in my .htaccess file like this

Code: Select all
Options -Indexes
RewriteEngine On

RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]


I noticed in your answer that your added a ? sign before the $ sign that indicate the end of the reg exp (for the first line) but not for the last line.. Like this

Code: Select all
RewriteRule ^([^/]+)/?$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]


I tried a couple of things with your code but apparently I'm missing something here...

$p1 always have the value index.php

Here's the link to a test page where you can view .htaccess file content and output

Can you help? Thanks a lot BTW!
jiv
 
Posts: 3
Joined: 10 May 2008 17:20

Re: RewriteRule / Regular expression problem

Postby mod_rewrite » 15 May 2008 04:58

Hey jiv thanks for providing the link to that page, very very cool my man! I'll try to help you out.

Code: Select all
Options -Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php.*\ HTTP/ [NC]
RewriteRule ^([^/]+)/?$ /index.php?p1=$1 [L]

RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php.*\ HTTP/ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?p1=$1&p2=$2 [L]

RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php.*\ HTTP/ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?p1=$1&p2=$2&p3=$3 [L]

RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php.*\ HTTP/ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]

RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php.*\ HTTP/ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]


RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php.*\ HTTP/ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\/]+)\ HTTP/ [NC]
RewriteRule .+ http://www.economik.com/%1/ [R=301,L]


How does this work?
mod_rewrite
 
Posts: 102
Joined: 30 Oct 2006 19:55

Re: RewriteRule / Regular expression problem

Postby jiv » 21 May 2008 15:48

Thanks mod_rewrite for the help

I didn't worked exactly as expected but however, I found the solution, mixing info found over the internet and my initial .htaccess file

Here's the way I solved my problem

Code: Select all
Options -Indexes

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/

RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]


Explanation

Doesn't apply the RewriteCond to an existing file on server
Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f


Doesn't apply the an URL that already ends with a slash
Code: Select all
RewriteCond %{REQUEST_URI} !(.*)/$


Add a slash at the end of the URL
Code: Select all
RewriteRule ^(.*)$ $1/


And here's the code to send values separated by slashes to index.php
Code: Select all
RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]


Hope it will help you guys!
jiv
 
Posts: 3
Joined: 10 May 2008 17:20


Return to Redirect or Rewrite Questions