简体   繁体   中英

Remove any trailing alphanumeric characters from URL

I have website in codeigniter. I am new to defining rule in .htaccess I want that if client types any alphanumeric character at end of URL those characters should be stripped off.

eg: In http://studio.com/gallery/babies/12abc

12abc should be removed.

Is there a way to do by setting a rule in .htaccess to remove any trailing alphanumeric characters?

I was searching rewrite rule for the same, but didnt find any. Please suggest.

If it's just alphanumerics you want to remove...

RewriteEngine On
RewriteCond   %{REQUEST_FILENAME} !-f
RewriteCond   %{REQUEST_FILENAME} !-d
RewriteRule   ^(([^/]+/)*)([a-z0-9]+)$    $1 [NC,R=301,L]

The two conditions ! -f ! -f and ! -d ! -d ensure the rewrite only happens if the URL doesn't point to a file (or directory) that actually exists - this prevents an URL that you actually want to go a specific, existent file/directory from being rewritten or you getting stuck in a horrible recursive loop causing Apache to throw a server error.

This .htaccess file needs to be at the root of your web server, if it's in a sub-directory you'll need to add the relevant RewriteBase .

The flags:

NC = No Case (makes the match case insensitive)

R=301 = makes this redirect an actual HTTP 301 redirect so that your it'll change the URI (thus removing the unwanted trailing alphanumerics)

L = Last, we're done processing just redirect without checking the rest of the RewriteRules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM