Home Removing trailing slash on custom urls with .htaccess (not wordpress)
Reply: 1

Removing trailing slash on custom urls with .htaccess (not wordpress)

logicK
1#
logicK Published in 2017-11-14 00:55:08Z

I'm new to learning HTACCESS and I'm trying my best to work on a websites URLs to make them more "seo friendly" and load properly. I've tried searching all over for what I need to point me in the right direction, but everything seems to be for "wordpress" and this is a custom built site.

Here is my issue. This site had a previous developer, the thing was a mess, and I've spent countless hours cleaning up the site, and he built the sites to show urls like this. "www.sitename.com/about-us.php"

I'm familiar enough with htaccess to know how to remove the trailing extension for .php, even showing trailing variables properly that doesn't require the trailing slash at the end like: "sitename.com/about-us?contact=true"

I was able to get the blog view page to load with a url like this: "sitename.com/posts/post-name-here/". This one though does require the trailing slash.

What I'm trying to accomplish is making the url string to work properly with trailing variables for a few pages like, register, apply, etc. So those urls would be something like this. "sitename.com/auth/apply" or "sitename.com/auth/register"

My issue is that the urls have to have a trailing slash at the end or it doesn't load.

Plus adding a trailing variables only works when formatted like this: "sitename.com/auth/apply/?application=submitted"

I'd prefer it look more like this. "sitename.com/auth/apply?application=submitted", without the trailing slash. But I can't find how to accomplish this.

Here is my .htaccess snippet.

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^posts\/(.*)/$ /view-post.php?post=$1 [QSA,NC,L]
RewriteRule ^auth\/(.*)/$ /auth-action.php?action=$1 [QSA,NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Can someone help me, or point me in the right direction?

Lag
2#
Lag Reply to 2017-11-14 10:14:48Z

Try using the following in your .htaccess. This will remove the trailing slash from your URLs.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=302,L]

Make sure you clear your cache before testing this. You'll notice that I've set R=302 instead of 301. This makes it a temporary redirect for testing purposes. If you're happy with the redirect then change it to 301 to make it permanent.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314865 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO