Redirecting Different Case URLs to a Base - apache

We have a compliance issue with a supplier as if you type the URL with incorrect capitals their brand is wrong. We are on a private apache server so can edit htaccess etc as needed.
The URL is /example/Brand-Name
We would like all the following variations to resolve back to /example/Brand-Name/ (as 301 redirects)
/example/BRAND-NAME/
/example/brand-name/
/example/Brand-name/
/example/brand-Name/
etc etc.
All the htaccess things I have tried have lead to infinite loops.
RewriteCond %{HTTP_HOST} /example/Brand-Name/$ [NC]
RewriteCond %{HTTP_HOST} !/example/Brand-Name/
RewriteRule ^(.*)$ /example/Brand-Name/$ [R=301,L]

Have you tried the following two things? Your NoCase statement as just brand-name (not spelled exactly as your correct version) and, switching the order of the rules.
RewriteCond %{HTTP_HOST} !^/example/Brand-Name/
RewriteCond %{HTTP_HOST} ^/example/brand-name/$ [NC]
RewriteRule ^(.*)$ /example/Brand-Name/$ [R=301,L]
I believe the way this works is like an AND statement in an IF. If Line1 AND Line2, then do rewrite rule. Try these adjustments and let me know. If that doesn't work I will do a bit of research for you.
Edit: A quick search and I see a lot of rewrites looking like this - !^/ - so perhaps you need to add the little roofy ^ guy after your !. I believe in rewrite this denotes the begging of where to match, and should probably be on the 2nd line as well.

Not sure how you got your rules to work at all, since your %{HTTP_HOST} var is never going to look like /example/Brand-Name/. You probably want:
RewriteCond %{REQUEST_URI} !^/example/Brand-Name/
RewriteCond %{REQUEST_URI} ^/example/brand-name/ [NC]
RewriteRule ^example/brand-name/(.*)$ /example/Brand-Name/$1 [L,NC,R=301]

Related

.htaccess RewriteRule gives right link but also a 404

I'm hosting different sites
http://example.nl/example.nl/_sites/byos/
http://example.nl/example.nl/_sites/eggbot/
http://example.nl/example.nl/_sites/hslab/
http://example.nl/example.nl/_sites/prolactin/
And yes there is a folder that has the same name as the domain, there is a reason for that.
And I want the links to become:
http://example.nl/byos/
http://example.nl/eggbot/
http://example.nl/hslab/
http://example.nl/prolactin/
This is one of the many attempts:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} !(.*)example.nl/_sites
RewriteRule ^(.*)$ example.nl/_sites/$1 [L]
And this one:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^example.nl/_sites/(.*)$ /$1 [L,NC,R]
The last one brings me close, it changes the address in what I want it to be, but it also results in a 404 now.
I also tried it with renaming the example.nl folder so it is not the same as the domain name but the problem seems to be the same.
In case it is important for later, I also have folders with files here:
http://example.nl/example.nl/_misc/
http://example.nl/example.nl/_plugins/
But I don't care if those get renamed, since they won't appear in the url bar, unless the user goes directly to one of those files, but I don't care about that.
So how can I omit the example.nl/_sites/ part and still have the website working?
I have seen the similar questions on SO, but for me it looks like Chinese in another dialect.
---- edit:
using the following of the answer from anubhava:
RewriteCond %{HTTP_HOST}::%{THE_REQUEST} ^(?:www\.)?([^:]+)::GET\s/+\1/_sites/(\S*)\s [NC]
RewriteRule ^ /%2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^[^/]+/?$ %1/_sites%{REQUEST_URI} [L]
I don't get a 404 anymore.
But this files for example:
http://hslab.nl/hslab.nl/_misc/bna.js
It tries to load it as:
http://hslab.nl/_misc/bna.js
Which fails. In the code it was targeted as:
src="../../_misc/bna.js"
In case it helps here is a screenshot of the folder hslab.nl:
Without hardcoding host name, you may try these rules in your site root .htaccess:
RewriteCond %{HTTP_HOST}::%{THE_REQUEST} ^(?:www\.)?([^:]+)::GET\s/+\1/_sites/(\S*)\s [NC]
RewriteRule ^ /%2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^[^/]+/?$ %1/_sites%{REQUEST_URI} [L]
With your shown samples, could you please try following. Fair warning I have written this in mobile so yet to test it should work IMHO will test it in sometime too. Also since you mentioned there could be multiple domains so I have specifically put a condition to check if it's example.nl here in case you want to rewrite request for any domain then we could omit that condition too.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^example\.nl$ [NC]
RewriteRule ^([a-zA-Z]+)/?$ %{HTTP_HOST}/_sites/$1 [L]
Note: in case you directories/folders are not necessarily starting with alphabets and could be anything then change regex in above from ^([a-zA-Z]+)/?$ TO ^([.*])/?$

What is the correct syntax for "if host is not foo, redirect to bar" in a .htaccess file?

This website has a ton of extra domains (note: these are not subdomains; one of them, for instance, is http://eduard.fi) that the owner (or the SEO people, rather) wants to redirect to the main domain. Instead of listing them one by one, this is what I tried:
RewriteCond %{HTTPS_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301,L]
However this creates a redirect loop. Why is that? This does not produce a server error, so for that part the syntax is correct, but it does not do what I want.
You were close, but made a logical mistake. Take a look at this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301]
An alternative would be that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^ https://masetti.fi%{REQUEST_URI} [R=301]
The RewriteCond has been slightly altered: It is the variable %{HTTP_HOST} you want to check, not %{HTTPS_HOST}which does not exist.
PS: it is a good idea to start out with a 302 redirection and only change that to a 301 once everything works as intended. That prevents issues with client side caching.

Escape string within RewriteRule

I am currently chewing on this problem: I am reworking a sort of CMS system, the result will be that URLs will look entire differently, but I want to save links and bookmarks, so I scrathced this .htaccess file together:
RewriteEngine on
# Rewrite old links to match with new URL syntax:
# universal rewrite
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ %1?action=redirect&source=$1&%3 [R=301,L,NE]
And it works, http://example.com/news.php?action=newsstory&nid=51 actually becomes http://example.com/?action=redirect&source=news&action=newsstory&nid=51
But you can see the problem, a double action. Using 'action' is not the most inventive term, but it is what the script I need to feed is working with, and also what I am getting from the old one, so I need to either:
replace the second 'action' with anything else
or serialize/escape the entire part which is defined by %3 from, the RewriteCond
In case you are wondering, the question mark required the use of a RewriteCond, couldn't get it to work any other way, so any solution that gets rid of it is just as welcome.
Well, thanks for looking into and maybe even shedding a light onto things, I just made my way into the whole regex thing, but havewn't entirely figured how variables are passed from chained RewriteRule to another, maybe that would have been the way to got, but try and error didn't get me anywhere.
Try removing the NE flag and use a B:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /?action=redirect&source=$1&%3 [R=301,L,B]
The only problem is that the %1 backreference will get encoded as well so I left that out. If you must have it there, you can try doing a two step rewrite:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /special-rewrite-here?action=redirect&source=$1&%3 [L,B]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ (.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^special-rewrite-here$ /%1 [L,R=301]
Fixed with help from Jon Lin:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /special-rewrite-here?action=redirect&source=$1&old_data=%3 [B]
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^/.+$ %1 [L,R=301]
I had to do the RewriteCond again to preserve %1, and had to modify the second RewriteRule slightly, but he sure got me on the right track, so: problem solved, the rest will take place in PHP, that should be no problem then.

htaccess RewriteRule for folder

I have a bunch of rewrite rules I would like to implement. I would like to redirect anything that has /blog/tag/... to my root url.
For example, all of these:
blog/tag/button-sets/
blog/tag/icons/
blog/tag/order-now/
blog/tag/body-attributes/
Would simply route to: www.url.com
I can do it on a case-by-case basis like below, but would like to redirect a bunch with 1 rule. Any help would be greatly appreciated
RewriteCond %{HTTP_HOST} ^url\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.url\.com$
RewriteRule ^blog\/tag\/button\-sets\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]
Why dont you just remove the button-sets part of your rule like so
RewriteRule ^blog\/tag\/?(.*)$ "http\:\/\/www\.url\.com\/" [R=301,L]
Havent tested it but should be OK?
In stead make it such that it captures everything after the second / after blog/tag/(ANYTHING)/(CAPTURE) which will redirect to www.url.com/CAPTURE
RewriteRule ^blog\/tag\/.*\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]

How to redirect direct hits

I'm sure this has been asked before, but I have no idea what it would be called.
I have done a quick Google and search here on SO.
Perhaps it would be better asked, how do I redirect http://site.com/shop to a specified URL like http://othersite.com/page without it affecting http://site.com/shop/?q=test or any other $_GET's.
RewriteRule ^shop http://othersite.com/page [R=301,L]
Works fine apart from it also affects links like http://site.com/shop/?q=test which I want to work.
Cheers.
You can use mod_rewrite to match against the query string (or to see if there is no query string):
RewriteEngine On
# blank query string
RewriteCond %{QUERY_STRING} ^$
# check host
RewriteCond %{HTTP_HOST} site\.com$ [NC]
RewriteRule ^shop/?$ http://othersite.com/page [R=301,L]