I have multiple domains in one virtual host like aliases: example.com is primary, example1.com and example2.com are aliases
I need to rewrite multiple domains into single entry point passing it via GET query param discaring the www prefix for example :
HOST -> rewrite to
www.example1.com/some/path -> [example.com]/index.php?q=domain/example1.com/some/path
example2.com/some/path -> [example.com]/index.php?q=domain/example2.com/some/path
my current .htaccess doesn't work as supposed
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(.*)/(.*) index.php?q=domain/$1/$2 [L,QSA]
Update1
Trying to use RewriteMap - rewrite.map it follows
example-site.com www.examplesite.com
www.example-site.com www.examplesite.com
.htaccess
RewriteMap host2site txt:/var/www/rewrite.map
RewriteRule ^(.*)$ index.php?q=/domain/{host2site:$1|NOTFOUND} [PT]
but it crashes with 500 server error :(
Try
RewriteCond %{HTTP_HOST} !=example.com [NC]
RewriteRule ^(.*)$ http://example.com/index.php?q=domain/%{HTTP_HOST}/$1 [L,QSA]
Related
and thanks to read me.
My goal is to have the same htaccess code in local and production.
First, I need to rewrite example.com/index.php?action=somepage to example.com/somepage.
Second, I must rewrite http://example.com to https://example.com, but only in production, not on localhost.
So far this is my code:
RewriteEngine on
RewriteBase /
# For Ionos
RewriteRule ^([0-9a-zA-Z/\ -]+)(?:&([0-9a-zA-Z&=_\ -]+))?$ index.php?action=$1&$2
# $1 : route name and framework parameters
# $2 : classic $_GET parameters (¶m=value)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
When I use https:// to connect, no problem, the first rule concerning index and action apply.
When I use http://, the adress become https://, BUT the index/action rule doesn't apply.
Thanks a lot!
Edit : this works :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301]
# For Ionos, http => https
RewriteRule ^([0-9a-zA-Z/\ -]+)(?:&([0-9a-zA-Z&=_\ -]+))?$ index.php?action=$1&$2
# $1 : route name and framework parameters
# $2 : classic $_GET parameters (¶m=value)
But I have a last problem.
My folder in like this:
[] example
...[]public
......htaccess
...htaccess
The code shown above is the htaccess of public directory. The htaccess of example directory redirect to public:
RewriteEngine on
RewriteBase /
# for Ionos
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
The problem is while http://example.com redirect to https://example.com, http://example.com/somepage redirect to https://example.com/public/somepage and I can't remove the "public" part.
Thanks!
Your two rules "were" in the wrong order. Your external redirect (HTTP to HTTPS) needs to be before the internal rewrite. Your first rule (rewrite) would have still applied, but it would have resulted in an external redirect to index.php?action=... (exposing your internal file/URL structure).
However, you are also missing L flags on both these rules.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301]
If this is in the /public/.htaccess file and the /public subdirectory is hidden (ie. it's being internally rewritten to) then you need to change the RewriteRule to read:
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
After the request is internally rewritten by the parent config, the REQUEST_URI server variable contains the full URL-path, including the "hidden" /public subdirectory, so if you redirect to REQUEST_URI it will expose the hidden subdirectory. Whereas, if you use a backreference to the matched RewriteRule pattern, which matches against a URL-path that is relative to the current directory then this naturally excludes the /public subdirectory.
I have a domain (e.g. doma.in) I am only using for forwarding purposes. So I created this .htaccess definition:
RewriteEngine On
RewriteRule ^(.*)$ http://www.mydomain.tld/in/$1 [R=301,L]
This successfully forwards calls in the following scheme
http://doma.in/abc -> http://www.mydomain.tld/in/abc
http://doma.in/123 -> http://www.mydomain.tld/in/123
http://doma.in/abc/123 -> http://www.mydomain.tld/in/abc/123
But I want to forward the root call, with or without www:
http://doma.in
http://www.doma.in
http://doma.in/
http://www.doma.in/
to the root URL of http://www.mydomain.tld.
I have tried to add another RewriteCond, but this setting throws a server error on all calls:
RewriteEngine On
RewriteRule ^(.*)$ http://www.mydomain.tld/o/$1 [R=301,L]
RewriteCond ^(.*)doma.in/?$
RewriteRule ^(.*)$ http://www.mydomain.tld [R=301,L]
How do I have to adapt my .htaccess in order to support my requirement?
You can use:
RewriteEngine On
# root URL
RewriteRule ^/?$ http://www.mydomain.tld/ [R=301,L]
# all but root URL
RewriteRule ^(.+)$ http://www.mydomain.tld/in/$1 [R=301,L]
Make sure to test this in a a new browser to avoid old 301 cache.
I have two domains that point to the same webserver:
example.cz
example.de
I need:
example.de → example.de/de (but can't be see in address field: example.cz/de)
If it ends up showing in the URL like example.de/de it's OK, but the best solution is just example.de and from server load example.de/de.
I've tried with this:
RewriteBase /
RewriteCond %{HTTP_HOST} example.de$ [NC]
RewriteRule ^$ example.de/de [L,R=301]
but after I click on something on the page I get: example.cz/de and this is problem.
You can use an inner rewrite without R=301 redirect, if you use LAMP server, in /.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} (www\.)?example\.de$ [NC]
RewriteRule ^$ /de/index.php [L]
You'll see in address bar: http://example.de but not http://example.de/de .
Add this to redirect http://example.cz/de to http://example.de .
RewriteCond %{HTTP_HOST} (www\.)?example\.cz$ [NC]
RewriteRule ^de$ http://example.de [R=301,L]
I want to rewrite one specific url.
http://example1.com should be http://example2.de .
But http://example1.com/subdir or http://sub.example1.com should remain the same.
I found the following, which successfully rewrites example1.com, but also every url which starts with example1.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Background: I want to redirect the main page of an WP-Multisite but want to make sure that I can work with the backend of wordpress and run other multisites which are subdomains.
For matching only http://example.com domain (without possibility to add anything before or after the example.com) use the following code:
RewriteCond %{HTTP_HOST} ^(example.com(\/{0,1})){1}$
RewriteRule http://example2.de(\/{0,1}) [R=301,L]
That (\/{0,1}) part is for matching both example.com and example.com/ (but nothing esle) - if you do not wish to match example.com/ remove that part from both rows.
You're pretty close but you don't need to capture URI in $1:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^$ http://example2.de/ [L,R=301]
I have a EC2 instance and host several sites on the server. I can redirect different domains to their specific folders but I cannot seem to redirect subdomains.
Here is my code so far for www.domain1.com which directs to "folder1" and retains the "www.domain1.com" address in the address bar - This is working fine:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule ^(.*)$ folder1/$1 [L]
However I cannot find a solution for sub-domains - I've tried many solutions similar to this - this does not work:
RewriteCond %{HTTP_HOST} sub.domain1.com
RewriteCond %{REQUEST_URI} !^/folder2
RewriteRule ^(.*)$ folder2/$1 [L]
I want http://sub.domain1.com to point to "folder2" but retain sub.domain1.com in the address bar. One thing I want to avoid is using my very long EC2 instance static address "http://ec2-12-34-567-890.compute-1.amazonaws.com/" in the code if this is possible.
Thanks
Try changing the host part to have boundaries:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
and
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
The first rule is matching against the host sub.domain1.com because it contains a domain1.com