Htaccess lowercase URL substring if found it in different case - .htaccess

I want to lowercase part of the URL string in .htaccess if it is not lowercase already.
Example,
http://www.example.com/This-Needs-to-Change/cars.aspx
to
http://www.example.com/this-needs-to-change/cars.aspx
What should be my Rewrite condition to change only if uppercase letter is found for that string (this-needs-to-change) only?

Define this RewriteMap in your httpd config file or in vhost config file:
RewriteMap lc int:tolower
Then in your site root .htaccess have this rule to lowercase first part before /:
RewriteEngine On
RewriteCond $1 [A-Z]
RewriteRule ^(this-needs-to-change)(/.*)?$ /${lc:$1}$2 [R=302,NC,NE,L]

Related

Convert route to lowercase incase it contains certain word [duplicate]

I need to create a redirect rule to lowercase.
All URLs starting with /catalog/ must be lowercase. example:
/catalog/Foo => /catalog/foo
/catalog/Foo/fOO2 => /catalog/foo/foo2
Can you help me?
Assuming you are on Apache 2.4 then you can use the tolower() function in an Apache expression in a mod_rewrite RewriteCond directive to perform the uppercase to lowercase conversion on these URLs.
For example:
RewriteEngine On
RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule ^catalog/.*[A-Z] %1 [R=302,L]
The RewriteRule pattern checks for a URL-path that starts catalog/ followed by an uppercase character. The RewriteCond directive then converts the URL-path to lowercase, which is captured in the regex and the corresponding backreference %1 is used in the RewriteRule substitution string as the target for the redirect. %1 contains the lowercase'd URL-path.

How to transform / remap URLs using .htaccess

I want a redirection in my website.
The old url looks like this :
http://www.example.com/Prem_Ratan_Dhan_Payo/Prem_Leela.html
Now we have to 301 redirect it to
http://example.com/songs/prem-leela/
In total I have to
Check if the url end with .html then we will redirect it other wise let it pass.
remove .html from end.
make all letters in lower case.
change underscore to hyphen.
change second segment to 'songs'.
Please help me.
This should get you started:
RewriteEngine On
# Replace underscores with hyphens in all requests ending in .html.
# The rule replaces one char at a time and is repeated until no matches are found.
RewriteCond "%{QUERY_STRING}" "\.html$" [NC]
RewriteRule ^([^_]*)_(.*) $1-$2 [N]
# Capture the file name without extension inside braces
# Use a lower-case mapping on the file name.
# Destination is /songs/filename
RewriteMap lc int:tolower
RewriteRule ^.*/(.*)\.html$ /songs/${lc:$1} [NC, R=301]

htaccess redirect urls to specific urls

I have Old URLs in my site and it like this:
http://'www.example.com/newsroom/press-release/item/949-ItemAlias.html
I want to redirect it to :
http://'www.example.com/press/ItemAlias.html
I used this sentence in .htaccess:
RewriteRule ^newsroom/press-release/item/(.*)$ /press/$1 [R=301,NC,L]
But this sentence redirect the URLs to http://'www.example.com/press/949-ItemAlias.html Of course the (949-) is example, I want to remove the number and dash when i redirect the URLs. What is the changes i have to do in .htaccess?
Your regex should match the number, but not include it in the ()'s. ex:
RewriteRule ^newsroom/press-release/item/[0-9]*-(.*)$ /press/$1 [R=301,NC,L]
The [0-9]* matches an arbitrary length number, followed by the -. Only what's matched parens will be included in $1.

.htaccess & 301: Redirect URL with capital letters to same URL but in lowercase

1) I got mysite.com/about-me.php.
When I type in mysite.com/About-me.php I get a 404 error.
I'd like to add some magic to my .htaccess so that mysite.com/My-PaGe.php (etc) redirects the user by 301, OR rewrites the URL (whatever is the best?) to mysite.com/my-page.php.
- Is 301 or rewrite the best?
2) Is is possibly for me to add one rule that applies to all URL's, or do I have to manually specify all different pages?
Bonus 3) Extra bonus star if anybody knows how to rewrite mysite.com/mY-paGe.php to mysite.com/my-site
Thanks alot
This does a 301 redirect, all urls that have an uppercase letter get rewritten to a lowercase version:
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
See also this article, in particular the comments section.
http://www.chrisabernethy.com/force-lower-case-urls-with-mod_rewrite/
You may need to specify this line in httpd.conf, and the RewriteCond / RewriteRule in .htaccess
RewriteMap lc int:tolower

htaccess rewrite ONLY if first part of path is numeric

Is there an htaccess rule that will only rewrite if the first part of a path is numeric, so that http://www.example.com/123/whatever hits the rewrite rule, but http://www.example.com/user/whatever does not?
Here is a rewrite rule for my little site I am building
RewriteEngine on
RewriteRule ([a-zA-Z])/ index.php?k=$1
RewriteRule ([0-9]+)/ index.php?id=$1
So you can see that the regex rule [0-9]+ will match any numbers successively. The [a-zA-Z] will match letters.
You can match numbers in your pattern. For example:
RewriteRule ^([0-9]+)/(.*) /foo/$2?bar=$1
Will rewrite http://www.example.com/123/whatever to http://www.example.com/foo/whatever?bar=123 but leave /user/whatever alone.

Resources