Rewrite URL requests for an .html extension to lowercase, remove extension and then add trailing slash
I'm working on a WordPress website that has some '.html' in the root folder (previous developer) and I need to
convert all requests made for a
.html
only
.
For a successful script it needs to:
-
remove
.html
extension
-
convert all chars to lowercase
-
add a trailing slash
-
(obviously) still be able to grab the requested file
This is my solution so far and
it does everything except convert the URL to lowercase
and also causes no conflicts to the WordPress URL structures:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1_$2.html [L]
EDIT:
I take it back. The above script didn't do what I needed it to do so I am open to all suggestions for this that use the
.htaccess
file in the root directory
Solutions
Adjust your
Apache configuration
to use mod_rewrites
RewriteMap
directives:
RewriteMap lc int:tolower
RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R]
See the Apache manual here: Redirect a URI to an all-lowercase.
Important note from the documentation:
Per-directory and .htaccess context
The RewriteMap directive may not
be used in sections or .htaccess files. You must declare
the map in server or virtualhost context. You may use the map, once
created, in your RewriteRule and RewriteCond directives in those
scopes. You just can't declare it in those scopes.
Similar questions
Forcing a trailing slash to a URL after a subdomain rewrite
I'm re-writing a subdomain to a 'folder' - actually a page in wordpress, and this all seems to be working correctly. I don't want the address in the URL bar to change though. Everything works fine unless the user does not put a trailing slash after the page name, then the page is still redirected to the correct URL but the URL in the address bar ch...
Find and replace URL's in database without trailing slash on end of URL
We're using WordPress for content management in our website. We have many internal and external links in our content. However, some people that placed the content made <a href""></a> URL's without adding a backslash to the end of the URL (Trailing Slash). Example: Find more info on <a href="https://test.com/testpage&qu...
htaccess: Remove trailing slash from URL ending with .xml/ only
I need to remove trailing slash from URLs ending with .xml/ only .. For this purpose I've created a Rewrite Condition and Rule which is working perfectly fine for the test link http://website.com/test.xml/ Test Link: http://htaccess.mwl.be?share=6fe08232-438a-53fa-8f1a-1f7f69b77b6f The problem is when I place the rule in WordPress .htaccess file, i...
Remove Trailing Slash from WordPress URL (The site also don't have www)
I need help as I am confused a lot with .htaccess Some months back, I removed WWW from the URL of my domain name using following .htaccess lines: My current .htaccess file looks as follows: Now, I also want to remove the trailing slash from the URL, as because I am using WordPress and a page/post will open, no matter if there's a trailing slash or ...
Remove trailing slash in wordpress blog URL
I decided to use wordpress to power my blog section. I created a folder under my root directory of my site titled blog. Now when I visit http://trekeffect.dev/blog I get redirected to http://trekeffect.dev/blog/ with the trailing slash. How can I remove the trailing slash? .htaccess inside blog folder is .htaccess i am using in my project rrot fold...
.htaccess - Remove trailing slash if site URL starts with a certain domain on
On WordPress, I have a few 301 redirects on my site that redirects the user to an external site Those 301 redirects have been adding a trailing slash, here's the code for it: So the above redirect redirects the user to https://example.com/survey/?query=12345/ which causes a 404 I would like to remove the trailing slash from all URLs if they start w...
Also ask