Ask Your Question
1

How can I use .htaccess to remove both ".php" and trailing slashes from my URLs?

asked 2023-06-19 05:08:51 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-19 05:38:02 +0000

lalupa gravatar image

To remove both ".php" and trailing slashes from URLs using .htaccess, you can use the following code:

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

# remove .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [L]

This code will first remove any trailing slashes from the URL, and then it will remove the ".php" extension from the end of the URL. Both of these changes will be applied using a 301 redirect, which is a permanent redirect that tells search engines that the URL has moved permanently.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 05:08:51 +0000

Seen: 10 times

Last updated: Jun 19 '23