mod_rewrite and redirect to the same domain with added parameters - apache

I've got a plenty of domains with A records set to the IP of my server.
Apache is processing all this domains with a single virtual host.
What I need is to redirect 'mydomain.com' to 'mydomain.com/?param=mydomain.com' with mod_rewrite, but I don't know how to do this.
I've tried this:
RewriteRule / /?param=%{SERVER_NAME}
with no result. I'd be grateful for any help.

If you really want to redirect example.com/ to example.com/?param=example.com you will need to use the R flag , try :
RewriteRule ^/?$ /?param=%{SERVER_NAME} [L,R]
And if you want to internally redirect example.com/ to example.com/?param=example.com try the following :
RewriteRule ^/?$ /?param=%{SERVER_NAME} [L]

I have solved the problem of eternal redirection by adding RewriteCond %{QUERY_STRING} ^$

Related

Apache 301 redirect adding port when using relative URL

Using Apache RewriteRule, I'm trying to do the following:
RewriteRule ^/foo /somedirectory/foo [NC,L,R=301]
The problem is that when it redirects, it is going to the following URL:
http://www.foo.com:81/somedirectory/foo
Is there a way to prevent the port from being written. I'm using a relative URL mainly because we have two sites (.com and .ca) that are on the same machines. I would normally do it like this:
RewriteRule ^/foo http://www.foo.com/somedirectory/foo [NC,L,R=301]
but if I am going to http://www.foo.ca/foo then it will redirect to the .com site.
I figured it out. Just need to do this:
RewriteCond "%{HTTP_HOST}" "\.ca"
RewriteRule ^/foo http://www.foo.ca/somedirectory/foo [NC,L,R=301]
RewriteRule ^/foo http://www.foo.com/somedirectory/foo [NC,L,R=301]
Just had to do the RewriteCond with the correct regex to check for the .ca.

301 redirection in htaccess not working

I have and issue when I'm trying to redirect my old domain to my new domain. Things look like that right now:
newdomain.pl <- is connected to blogger account
olddomain.pl <- is connected to server with .htaccess file (before it was connected to blogger account)
I would like it to work like that : olddomain.pl/subdomain -> redirect to newdomain.pl/subdomain. But whatever I try to put into my .htaccess file it's redirecting my old site to main page of newdomain (newdomain.pl). I've tried codess like that:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^newdomain.pl[nc]
RewriteRule ^(.*)$ http://www.newdomain.pl/$1 [r=301,nc]
but it's also redirecting to main page of new domain. Only when I put:
RedirectRule / http://www.newdomain.pl
It's redirecting to http://www.newdomain.plsubdomain <- but there is slash "/" missing between the newdomain name and subdomain. Unfortunately when I write this:
RedirectRule / http://www.newdomain.pl/
it's redirecting to main domain page http://www.newdomain.pl, so it's not working. I don't know what can be wrong, I'm fighting with that three days already. Does anyone have an idea where can be the problem? Maybe there is something wrong with hosting that I bought? Thank you in advance for any response
Regards, Pawel
Try next:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R]
For only redirecting queries without direct redirecting to domain remove R flag.
Try this in olddomain/.htaccess :
RedirectMatch ^/(.*)$ http://newdomain.com/$1
Ok I found a solution. The problem was not including "www" in RewriteRule. When I changed it, it worked. I'm putting the code that worked for me below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.pl$
RewriteRule ^(.*)$ http://www.newdomain.pl/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^olddomain.pl$
RewriteRule ^(.*)$ http://www.newdomain.pl/$1 [L,R=301]
Thank you everyone!

Apache redirect from one subdomain to another

Does anyone know a way to do a permanent redirect from a.example.com to b.example.com? I have other subdomains that need to remain as they are though. so:
first.example.com -> second.example.com
third.example.com is fine
fourth.example.com is fine
I want to do this in the <VirtualHost> block rather than an .htaccess to avoid having to redeploy again.
Any thoughts would be appreciated, thanks
Add the following RewriteRule to your VirtualHost
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule ^ http://second.example.com [R=301,L]
If you wanted to redirect first.example.com/some/url to second.example.com/some/url:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule /(.*) http://second.example.com/$1 [R=301,L]
Recommend you use [R=302] whilst testing the rules to avoid problems with 301s getting cached by your browser.

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

Apache mod_rewrite: can these simple RewriteRule be improved? And suggestions!

I started finally to understand Apache mod_rewrite. It's pretty GREAT!
Plz have a look at the followings:
1) Permanent redirects http://www.domain.com/folder_name/ (with or without final slash and with or without the www) to http://www.domain.com/some/path/some_page.html
RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]
2) Permanent redirects all requests to www.domain.com... to same path and file request but without www in domain
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
They all work as expected and do their jobs, I'm simply curios if some guy, who is more expert than me in mod_rewrite, could give me some advises like: "it could be better in this way...", "there might be a problem if...", etc.
Thanks!
Use the ? quantifier instead of * and you don’t need to escape the substitution URL:
RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
You might want to consider HTTP 1.0 requests where the Host header field is missing. Another useful extension would be to take HTTPS into account:
RewriteCond %{HTTP_HOST} !^(|example\.com)$
RewriteCond %{HTTPS} ^on(s)|
RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]