1

I would like to add a trailing slash for certain static page types like .html, .asp and .pdf.

I tried the following code in .htaccess but it is adding a trailing slash to all types of links.

# Force trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]

My site bought from a previous owner has many .html, .asp and .pdf pages which are now showing server errors in Google webmaster tools. Now I am using WordPress default permalink structure and with the redirection plugin, those static pages are not redirecting unless I force a trailing slash on them.

  • "now showing server errors" - presumably you mean 404s? (Although these aren't strictly "server errors".) – MrWhite Feb 12 at 22:18
0

Redirecting to append a trailing slash so you can use the "redirection plugin" to redirect these requests again doesn't make a whole lot of sense. (This would seem to suggest that these "static pages" still exist? Although the directives you posted suggest otherwise?)

Anyway, to answer your question (assuming these pages don't exist)...

Your current RewriteRule targets every URL (ie. (.*)), so it naturally appends a trailing slash to all URLs. You need to target just the URLs that end .html, .asp and .pdf. And if you do this you don't need the condition that checks that a trailing slash is not already present on the URL-path.

I don't think you really need to check the REQUEST_METHOD either?

So, this could be written:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+\.(?:html|asp|pdf))$ /$1/ [L,R=301]
  • 1
    Awesome, your code is working perfectly. :) Thanks for your nice help.Yes you are correct, those static pages do not exist and I am redirecting some valuable pages of them to get their link value. – Taswir Haider Feb 13 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.