Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache RewriteRule requires trailing slash at the end of url to work

Ok so i have a url like

domain.com/item/item_id/item_description/page

when i type the link without

/page

on the url it throws a 404 error and i have to type the trailing slash on the url to make it work..

this is my htaccess code

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^item/([0-9]+)/(.*)/(.*)/?$ item.php?action=item&id=$1&desc=$2&page=$3

i have found this after searching:

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]

which kinda solves my problem but how can i make the trailing slash to be optional by the user if the user wants to add it or not so it wont redirect everytime a slash is not found

like image 939
stergosz Avatar asked Jun 12 '11 08:06

stergosz


1 Answers

You can handle the request using one rewriterule.

RewriteRule ^item(?:\.php)/([0-9]+)/([^/]+)?/?([^/]+)?/?$ item.php?action=item&id=$1&desc=$2&page=$3 [L]

Please note I have added (?:\.php) before ^item, just to be sure this rewriterule works, if your webserver for some reason convert request

 domain.com/item/...

into

 domain.com/item.php/...

Tip: you can see your current rewriterule behavior enabling RewriteLog:

RewriteLogLevel 9
RewriteLog "/var/log/apache2/dummy-host.example.com-rewrite_log"

Be careful do not use this in production.

like image 183
freedev Avatar answered Sep 27 '22 22:09

freedev