Rewrite Trailing Slash

Here is my .htaccess file

Options +FollowSymlinks RewriteEngine on ErrorDocument 404 /404.php RewriteRule ^(\d*)/(.*) /page.php?id=$1&slug=$2 

Everything works perfectly. But the moment I type site.com/342/my-page/(with the trailing slash), I get 404.

I need a trailing slash as optional. I. It will redirect to the correct page with or without a slash.

I tried this but it did not work

 RewriteRule ^(\d*)/(.*)/?$ /page.php?id=$1&slug=$2 

Any ideas?

+4
source share
1 answer

. * greedy, so he will eat up your slashes, even if it's not necessary. You should make him stay away:

 RewriteRule ^(\d*)/(.*[^/])/?$ /page.php?id=$1&slug=$2 

This ensures that $ 2 never ends with a slash.

+6
source

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


All Articles