Redirect with htaccess unless search - .htaccess

I'm not sure if this is possible, but here's whats going on.
I currently have an .htaccess rule to redirect example.com to store.example.com
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://store.example.com/[L,R=301]
This works great, however my search queries no longer work. What I'd like to do is redirect example.com to store.example.com unless I'm searching.
A sample search would look like this:
http://example.com/?s=my+search+query+string
Thank you in advanced for your help

Have you tried:
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{QUERY_STRING} ^!(s=(.*))$
Rewriterule ^(.*)$ http://store.example.com/ [L,R=301]

Related

Using ISAPI-Rewrite htaccess to 301 redirect multiple urls for multiple hostnames?

I have a single site within IIS, which runs on 4 different hostnames. The CMS handles that, and displays the correct site based on the incoming hostname.
I need to use ISAPI rewrite to handle all the old urls and 301 redirect them to the new equivalants, this is how I currently deal with redirects in my ISAPI-rewrite .htaccess file
RewriteEngine on
RewriteRule ^post/my-old-page-one$ /my-newer-page-one [R=301]
RewriteRule ^post/my-old-page-two$ /my-newer-page-two [R=301]
My issue is, I need it to check the domain too for the incoming url. As the sites have the same old urls, which now need to go to a different page.
I was hoping I could so this.
RewriteEngine on
RewriteRule ^http://www.siteone.com/post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301]
RewriteRule ^http://www.siteone.com/post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301]
But it doesn't work. Any advice greatly appreciated.
The check of the hostname is performed in a separate condition, like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301,NC,L]

.htaccess rewrite multiple urls without redirects

Ok so i have urls like these demo1.domain.net, demo2.domain.net, demo3.domain.net and all needs to show the contents of a subfolder like this example
demo1.domain.net will show the contents of domain.net/websites/demo1
i need to do it in a way that all the domains like $whatever.domain.net into domain.net/websites/$whatever.domain.net, i already enabled wildcard dns, and i cant seem to fiqure it out, please keep in mind i dont want it to redirect.
Here is what i tried
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.domain\.net.*$
RewriteRule /websites/$1 [L]
I just get the main page of domain.net with this.
Your help will be greatly appreciated.
Close, try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/websites/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.domain\.net.*)$ [NC]
RewriteRule ^(.*)$ /websites/%2/$1 [L]
Note that this will only work if the all the demo*.domain.net point to the same document root as the main domain (domain.net).
I think, I can manage this! Could you please try these .htaccess directives AGAIN:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^/?$ http://domain.net/websites/%2 [P]
It will rewrite $whatever.domain.net into domain.net/websites/$whatever.
NOW if it's suppose to be $whatever.domain.net into domain.net/websites/$whatever.domain.net then you could try this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^/?$ http://domain.net/websites/%2.domain.net [P]
Or maybe this code below is what you've wanted in the future:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^(.*)$ http://domain.net/websites/%2.domain.net/$1 [P]

htaccess, url must have the www in it or re-direct

I need to re-direct a url of a website of mine, if it hasn't the www in it, because of a script that dont work if the url isn't complete, how can i do this with a htacces file, I cant find an example on the web.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Something like this might work for you:
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http://www.%1%2 [R=301,L]

301 redirect without index

I saw many 301 questions but I haven't found a solution for mine so I'll just post it here.
The standard 301 looks something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.301redirect\.de$
RewriteRule ^(.*)$ http://www.301redirect.de/$1 [L,R=301]
Well, when I surf to my website I end up on www.domain.tld/index.php/restofquery. I want to get to www.domain.tld/ when I surf to domain.tld and I also want no index.php in any other query. So domain.tld/articlebla should be www.domain.tld/articlebla.
Any way to accomplish this?
Best regards.
To simply add on the www you would do:
RewriteCond %{http_host} ^301redirect\.de$ [NC]
RewriteRule ^/?$ http://www.301redirect.de/ [R=301,NC]
To also add on the other urls parts and www, you would do:
RewriteCond %{http_host} ^301redirect\.de$ [NC]
RewriteRule ^(.*?)/?$ http://www.301redirect.de/$1/ [R=301,NC]
If you mean you don't want the original query string on the redirect url then this will work:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.301redirect\.de$
RewriteRule ^(.*)$ http://www.301redirect.de/$1/? [L,R=301]
Adding a ? to the end of the RewriteRule means that the query string won't get added.

Redirect root of parked domain in one way, all other urls in another way, using htaccess

I recently got an alternate domain (parked) and would like it to behave like this:
on a request for "alt-domain.com/" (root): serve "main-domain.com/fakeroot" without a redirect (200)
on a request for "alt-domain.com/anyotherpage": serve "main-domain.com/anyotherpage" with a redirect (301)
I tried this, but it doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
Each rule works on its own, but when both are present the request for root also get a redirect. I tried inverting the order, tried skipping [S=1], tried [NS], but no luck. That's when I realized I'm not an htaccess expert, nor a regex expert.
Any help would be much appreciated.
D.
The problem is that the L flag probably doesn't work like you expect, and when mod_rewrite re-examines your rules, RewriteCond %{REQUEST_URI} !^/$ matches because the %{REQUEST_URI} is updated to /fakeroot.php after the initial rewrite.
There are a few different ways to fix this, but I believe this should work well enough, and it doesn't involve changing much:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/(fakeroot\.php)?$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]

Resources