Apache RewriteRule requires a trailing slash at the end of the 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 url it throws error 404, and I have to enter a trailing slash on url for it to 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 found this after searching:

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

which kind of solves my problem, but how to make the trailing slash optional for the user if the user wants to add it or not, so it won’t be redirected every time the slash is not found.

+6
source share
2 answers

You can process the request using one rewriting file.

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

Please note: I added (?:\.php) to ^item , just to make sure this rewriting file works if your web server converts the request for some reason

  domain.com/item/... 

in

  domain.com/item.php/... 

Tip. You can see the current rewriterule behavior allowing RewriteLog:

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

Be careful not to use this in the manufacturing process.

+1
source

Use two rewrite rules (one with one and without a "page"):

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

Source: https://habr.com/ru/post/890331/


All Articles