.htaccess for regular expression needs to do trailing slash

I need to include an optional trailing list, which /, in my RewriteRule

What am i still

RewriteRule ^([a-zA-Z0-9]+)$ u.php?$1|$2

Which works fine, for example http://foo.bar/abcde will be redirected to http://foo.bar/u.php?abcde and processes any request parameters that may be present.

I need to do http://foo.bar/abcde/ (with a trailing file) and redirect to http://foo.bar/u.php?abcde

So, if its real, I need to remove the final slash from $ 1 in the RewriteRule. How can I do it? I am new to apache and have tried many different regex rules but can't figure it out.

+5
source share
1 answer

Just put /?in front of $the end of your template:

RewriteRule ^([a-zA-Z0-9]+)/?$ u.php?$1

But I would prefer that you allow only one spelling (with or without a trailing slash) and redirect another:

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
+14
source

All Articles