301 redirect but change URL to all lower case in apache - apache

I'd like to set up a 301 redirect in Apache to change the case of the original address before redirecting. So for example if someone enters:
www.website1.com/UsErOfMiXedCase
it should forward to
www.website2.com/userofmixedcase
Would this be a redirect or would it need to be a rewrite? I'm not fussed about individual page forwarding (eg www.website2.com/userofmixedcase/whatever.php) - just www.website1.com/whatever.
Thank you in advance,
Richard

You need to define the rewrite map using Apache's internal tolower function. This can only be done in vhost or server config, and will result in an error if you try to put these directives in an htaccess file:
RewriteEngine On
RewriteMap lowercase int:tolower
Then, in your htaccess file, you can use something like this above any rewrite rules you already have. The redirect rules must be before whatever rules you may have that does routing:
# check that the lower case version of the URI is different than the unchanged URI
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteCond ${lowercase:%1}::%1 !^(.*)::\1$
RewriteRule ^/?(.+)$ http://www.website2.com/${lowercase:$1} [L,R=301]
This will redirect a request like http://www.website1.com/UsErOfMiXedCase to http://www.website2.com/userofmixedcase, thus replacing the URL in the browser's address bar with the one that's all lowercase. Note that it won't affect URLs with multiple path nodes, e.g. http://www.website1.com/SoMe/PathName/UsErOfMiXedCase. If you want it to affect all requests including ones that have multiple paths/subdirectories, then you need to change this line:
RewriteCond %{REQUEST_URI} ^/([^/]+)$
to:
RewriteCond %{REQUEST_URI} ^/(.+)$

You are wanting to use mod-rewrite for this. Something like:
RewriteRule ^/(.*) http://website2.com/$1 [NC]
That is a very general rule, so anything after the leading slash will be lower case. You may want to only do the lower case for specific portions of URL, but I cannot speak to that.
You probably should eyeball the Apache mod-rewrite documentation about the NC flag about this as well.
hth!

Related

Apache mod_rewrite links proxy

I want to make a proxy for external links with apache's mod_rewrite module.
I want it to redirect user from, ie http://stackoverflow.com/go/http://example.com/ to http://example.com/ where http://stackoverflow.com/ is my site's URL. So I added a rule to .htaccess file.
RewriteRule ^/go/http://(.+) http://$1 [R=302,L]
But it doesn't work at all. How to fix this?
I am not sure if Apache or the browser reduces // to /, but since it doesn't change the directory one of them reduces this to a single slash on my setup. That's why the second slash has a ? behind it in the rule below:
RewriteRule ^go/http://?(.*)$ http://$1 [R,L]
This will redirect the user to that domain.
This will rewrite all urls (without the beginning http://) to new complete URL. If you're gonna use https links also, you need something like the second rule.
RewriteRule ^go/(.*) http://$1 [R=302,L,QSA,NE]
RewriteRule ^gos/(.*) https://$1 [R=302,L,QSA,NE]
I also added the QSA if your need to include parameters

.htaccess 301 redirects based on TLD

We're in the process of switching our current site from a multiple domain configuration into a single domain, multiple folder format. i.e.
.co.uk/<uri> is becoming .com/en-gb/<uri>
.com/<uri> is becoming .com/en-us/<uri>
I'm hoping that I'll be able to handle this via a couple of well-crafted .htaccess rules, but I'm not sure of code I'm going to need to achieve this. Can you help?
(PS, I've left the actual domain blank, as we only need to test for the TDL, not the entire domain - although whatever the original domain was need to stay the same, with only the TLD changing - i.e. whatever.co.uk would redirect to whatever.com/en-gb/, whatever2.co.uk would redirect to whatever2.com/en-gb/, and whatever.com would redirect to whatever.com/en-us/)
add the following directives to your .htaccess:
RewriteCond %{HTTP_HOST} (.+)\.co\.uk$
RewriteRule (.*) http://%1.com/en-gb/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/(en-us|en-gb)/
RewriteRule (.*) /en-us/$1 [R=301,L,QSA]
if you have so many domain TLDs, you maybe want to use RewriteMap to avoid duplicating the first rule for every TLD, RewriteMap will map TLD to Uri string (ex: .co.uk to en-gb),

Why does mod_rewrite strip the port number from my request url?

I'm using Apache mod_proxy with mod_rewrite to mask some credentials in the query string. The logs show that the rewrite rule is working properly, but the port number (:9000) is being stripped causing the proxy to reject the URL. Aside from security lectures and anything else irrelevant to the actual question at hand, can anyone tell me what the solution might be?
Basic example using:
RewriteCond %{QUERY_STRING} ^(.*)userid=fakepass(.*)
RewriteRule ^(.*)$ $1?%1userid=realpass%2
Original URL: https://domain.com:9000/somedirectory/request.jsp?userid=theuser&password=fakepass
Intended URL:: https://domain.com:9000/somedirectory/request.jsp?userid=theuser&password=realpass
Actual result after mod_rewrite: https://domain.com/somedirectory/request.jsp?userid=theuser&password=realpass
Specify the port in your rule. Change your .htaccess to something like:
RewriteCond %{QUERY_STRING} ^(.*)userid=fakepass(.*)
RewriteRule ^(.*)$ https://%{HTTP_HOST}:9000/somedirectory/request.jsp?userid=theuser&password=realpass

Making sure my rewrite rule is a 301 re-direct

I have the following re-write rule which directs krmmalik.com to krmmalik.com/me
How do i make sure this rule is a 301 re-direct, and if it isnt one already, how can i turn it into one?
I've tried using the mixing and matching the tips from this site
http://www.webweaver.nu/html-tips/web-redirection.shtml
as well as Google's Support Articles and existing SO questions, but not having much luck. Note the re-write rule in itself so far has been working fine.
I've also added a CNAME for "www" to "krmmalik.com" in my DNS file. Is that good enough, or do i need to add a specific 301 redirect for that as well?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?krmmalik.com$
RewriteRule ^(/)?$ me [L]
Try the following:
RewriteRule ^/?$ /me [L,R=permanent]
The R=permanent flag instructs a 301 status redirect (and you can use R=301 if you prefer, but I think that "permanent" is more readable).
Putting a forward-slash at the start of the /me target URL will tell Apache to redirect the user to the directory named "me" at the web server's public root directory. So in your case it should redirect the user to krmmalik.com/me (or www.krmmalik.com/me).
Also, you don't need to wrap the match pattern in parentheses, because you don't need to capture the slash for later use. So ^/?$ will do the job fine.

Simple mod_rewrite RewriteRule for messy legacy url's

Launching a new website for a new client. Their old site has about 50 products and unfortunately, the old product names do not match up to the new URL pattern
Old URL Examples:
example.com/products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription
example.com/products.aspx?category=Foo&product=ProductNameDescription&var1=1293.123
example.com/products.aspx?category=Bar&product=ProductCategoryProdNameRandomNumbers
(The old URL's are sometimes hitting 150+ characters.)
New URL's:
example.com/products/category/actual-product-name
There's no set, recognizable pattern to go from the old product name to the new one. There is for the category.
I've tried simple mod_alias Redirects, but understand that I need a RewriteRule instead. But I'm having problems. All I need is a 1-to-1 redirect for each of these 50 URL's. I thought I could do something like:
RewriteRule ^/products.aspx?category=Foo&product=ProductName
/products/category/new-product-name/ [R=301,NC]
But that isn't working. I know this should be simple, but I am stuck. Any ideas?
Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g. ? , . and space by adding a \ in front of them
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.aspx\?category=Foo&product=SuperLongNoBreakProductNameIDDescription [NC]
RewriteRule ^ /products/category/new-product-name/ [R=301,NC]
Have a look at the RewriteMap directive of mod_rewrite.
You can specify in a text file something like:
products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription /products/category/new-product-name
And in your httpd.conf
RewriteMap productmap /path/to/map/file.txt
RewriteRule ^(.*) ${productmap:$1} [R=301,NC]
Tip: If it's a permanent redirect you want, make sure you set an appropriate Cache-Control and Expires header to instruct browsers to cache the 301.
You can try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^category=Foo&product=ProductName$
RewriteRule ^products\.aspx$ /products/category/new-product-name/? [R=301,L]
Notes:
In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the RewriteRule pattern with ^/.
You have to use RewriteCond to match against the query string.
As stated in another answer, a RewriteMap solution might be suited to this situation, if you have access to httpd.conf / the vhost definition for this site. I'm not sure how that works with query strings though.
For something like this, it might be a better solution to rewrite all of these URLs to a server side script, and use the script to do the HTTP redirect for each URL.