简体   繁体   中英

How remove a trailing slash by using htacces

we have a problem. guys. Maybe someone dealt with something like this. Any help will be much appreciated.

We decided to make a new website. Every webpage that we made, that is stored on the server looks like this: „example.php” However, we didn't want every single url of ours to look like: „company.com/example.php”, so we added this code to .htcaccess, marked as „# cut url”:

RewriteEngine on
# cut url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

Works perfectly. Removes the „.php” part.

However there is a problem. The previous version of our website used wordpress, therefore every single link, that can be found on the internet and leads to our company's site, including past blog entries have a slash at the end of each URL, ie „company.com/stuff/„, we copied the content for our new blog, so every entry looks like this „company.com/stuff.php”, by using the code i have specified above, we managed to remove the „.php” part. So whenever anyone accesses the blog entry, he sees „company.com/stuff” and here's the problem.

We would like to add something in .htcaccess that would help us redirect the old URLs, including those indexed by google. So that if anyone finds any our old links with a slash at the end, and clicks it will be automatically redirected to the blog entry with the same content on our new website. So let's say he clicks on „company.com/stuff/„ and it automatically takes him to „company.com/stuff” (actually to „company.com/stuff.php” but the aforementioned code removes the „php” part.)

And we're not sure how to do that. We've been trying to find solutions, some people suggested a piece of code, that removes the trailing slash, however it does not work, i put it here and it's marked as „remove trailing slash”.

Please, maybe someone has a solution. We have standalone php, no frameworks.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^\.])+/?$ $1.php [NC,L]

You probably want to add a R=301 flag in there as well, but I'm not sure - that's up to you.

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