skip to Main Content

Can’t seem to get this right for my .htaccess file.

Want to change the spelling in a part of a URL. In the middle of the URL it is /Johannsen/ and I want to change it to /johannsen/ e.g. lower case.

2

Answers


  1. If you have control over your vhosts through vhosts.conf or vhosts file, you can use RewriteMap.

    1. in vhosts.conf, we activate the int: Internal Function, defining a RewriteMap named maplower, as we cannot use RewriteMap directive in .htaccess file:
    
    #
    RewriteMap maplower int:tolower
    #
    

    Restart apache2, under debian OS, we do it with

    
    /etc/init.d/apache2 restart
    
    1. Then in you directory/.htaccess, we use the previously defined RewriteMap as this:
    
    RewriteEngine On
    RewriteCond %{REQUEST_URI} [A-Z]
    RewriteRule . "${maplower:%{REQUEST_URI}}" [R]
    

    It does this: with RewriteCond, we look at the %{REQUEST_URI} if there is any A-Z uppercase letter. If yes, redirect to an all lowercase url.

    For example,

    https://www.example.com/test/Johannsen =>
    https://www.example.com/test/johannsen

    https://www.example.com/test/NewDAte.php =>
    https://www.example.com/test/newdate.php

    Login or Signup to reply.
  2. This would seem to be a straightforward string replacement. The fact that you happen to be changing the first letter of the word to lowercase is irrelevant since this would seem to be the only word you are changing.

    You can do this using mod_rewrite near the top of your .htaccess file:

    RewriteEngine On
    
    # Convert/redirect "/Johannsen/" to "/johannsen"
    RewriteRule ^(.*/)J(ohannsen/.*) /$1j$2 [R=302,L]
    

    This will redirect a URL of the form /foo/Johannsen/bar to /foo/johannsen/bar. The $1 and $2 backreferences capture the part of the URL-path before and after the J respectively.

    If this is intended to be a permanent redirect then change the 302 (temporary) to 301 (permanent) once you have confirmed that it works OK.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search