htaccess Elite

Hire htaccesselite professional

.htaccess tutorial


All times are UTC - 5 hours





Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: RewriteRule / Regular expression problem
PostPosted: May 10th, '08, 11:27 

Joined: May 10th, '08, 11:20
Posts: 3
Hi All

I have a problem. Here's is my RewriteRule

Code:
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!


Top
 Profile  
 
 Post subject: Re: RewriteRule / Regular expression problem
PostPosted: May 10th, '08, 22:17 

Joined: Oct 30th, '06, 13:55
Posts: 89
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:
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!


Top
 Profile  
 
 Post subject: Re: RewriteRule / Regular expression problem
PostPosted: May 12th, '08, 09:37 

Joined: May 10th, '08, 11:20
Posts: 3
Thanks for the reply mod_rewrite

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

Code:
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:
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!


Top
 Profile  
 
 Post subject: Re: RewriteRule / Regular expression problem
PostPosted: May 14th, '08, 22:58 

Joined: Oct 30th, '06, 13:55
Posts: 89
Hey jiv thanks for providing the link to that page, very very cool my man! I'll try to help you out.

Code:
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?


Top
 Profile  
 
 Post subject: Re: RewriteRule / Regular expression problem
PostPosted: May 21st, '08, 09:48 

Joined: May 10th, '08, 11:20
Posts: 3
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:
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:
RewriteCond %{REQUEST_FILENAME} !-f


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


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


And here's the code to send values separated by slashes to index.php
Code:
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB