Little help with rewrite..

Discuss practical ways rearrange URLs using mod_rewrite.

Little help with rewrite..

Postby Pitt » Fri Jul 18, 2008 9:13 am

Hello, i have this problem.
I have this peace of code:
Code: Select all
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([^/]+)-([0-9]+)$ /index.php?action=$1&no=$2           [QSA,L]
RewriteRule ^([^/]+)-([0-9]+)/$ /index.php?action=$1&no=$2           [QSA,L]
RewriteRule ^([^/]+)-([0-9]+)-$ /index.php?action=$1&no=$2           [QSA,L]

RewriteRule ^([^/]+)-([0-9]+)-([^/]+)$ /index.php?action=$1&no=$2           [QSA,L]
RewriteRule ^([^/]+)-([0-9]+)-([^/]+)/$ /index.php?action=$1&no=$2           [QSA,L]

ErrorDocument 404 "http://www.example.com/404.php"


And now.. I wanted to sort not to be redirected into page 404.php. So all scenarios are ok but theres problem with those ones:
Code: Select all
http://www.example.com/detail

Code: Select all
http://www.example.com/detail/

Code: Select all
http://www.example.com/detail-


I tryed to add those lines
Code: Select all
#RewriteRule ^([^/]+)$ /index.php?action=$1          [QSA,L]
#RewriteRule ^([^/]+)/$ /index.php?action=$1         [QSA,L]
#RewriteRule ^([^/]+)-$ /index.php?action=$1         [QSA,L]

but server says error 500.
Thank you for helping. Or i just need "to show the door".:)
Pitt
 
Posts: 2
Joined: Fri Jul 18, 2008 9:06 am

Postby richardk » Fri Jul 18, 2008 3:16 pm

Code: Select all
#RewriteRule ^([^/]+)$ /index.php?action=$1          [QSA,L]

This rule is causing the 500 error. It matches index.php requests and causes a loop.

Try
Code: Select all
Options +FollowSymLinks
ErrorDocument 404 "http://www.example.com/404.php"

RewriteEngine On

# Ignore existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(-|/)?$ /index.php?action=$1 [QSA,L]

RewriteRule ^([^/]+)-([0-9]+)(-|/)?$ /index.php?action=$1&no=$2 [QSA,L]

RewriteRule ^([^/]+)-([0-9]+)-([^/]+)/?$ /index.php?action=$1&no=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Pitt » Fri Jul 18, 2008 6:25 pm

richardk:
I tried your script and there was problem with the first variable:
Code: Select all
([^/]+)

When i wrote anything i got just the "action" variable but no variable "no".
Like:
Code: Select all
http://www.example.com/detail-8-huuuuu-huuu
>> action='detail-8-huuuuu-huuu' and no=''

So here is the final version:
Code: Select all
Options +FollowSymLinks
ErrorDocument 404 "http://www.example.com/404.php"

RewriteEngine On

# Ignore existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z]+)(-|/)?$ /index.php?action=$1 [QSA,L]

RewriteRule ^([a-z]+)-([0-9]+)(-|/)?$ /index.php?action=$1&no=$2 [QSA,L]

RewriteRule ^([a-z]+)-([0-9]+)-([^/]+)/?$ /index.php?action=$1&no=$2 [QSA,L]

This is what i call "showing the door" or the way. Whatever.:)
Thank you for your help.
Pitt
 
Posts: 2
Joined: Fri Jul 18, 2008 9:06 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 104 guests

cron