i want to rewrite
example.com to www.example.com
My Code in apache2.conf (Main config file of apache in Linux/ubuntu 32bit ):
try 1:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Restart Apache Code Doesn't Work !
try 2:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
Restart Apache Code Doesn't Work !
try 3:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Restart Apache Code Doesn't Work !
Loaded Modules in apache displays:
rewrite_module (shared)
How can i solve this ?
And is there any other mod i have to enable(I don't think so).
I have to put this code in some other file ?
It is neccessary/must use .htaccess for url rewriting ?
Your rules are fine.
Make sure the rules are in the virtualhost of the "example.com" domain. If you have no "example.com" vhost, then place the rules in the first vhost container. S
<VirtualHost *:80>
ServerName example.com
....
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
....
</VirtualHost>
Try putting the rules in a directory container:
<Directory "/var/www/path/of/your/document_root/">
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</Directory>
Related
I'm sorry to post a new question for something that has been asked many times but none of the answers I found worked for me.
I have a site in Symfony and I want it to use HTTPS and not HTTP, so I want to redirect the URLs.
For this, I have a .htaccess file in the public:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
And another in the racine, I've tried multiple things but none worked, here are some of my tests:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Or
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]
or
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://myurl [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?10.16.10.164)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ web/$1 [QSA,L]
Unfortunately, I'm not sure to understand right all of these and this is maybe why I'm not able to make it work.
There is other information you will maybe need:
My apache conf has this:
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}:8000%{REQUEST_URI}"
</Location>
</VirtualHost>
or
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*)$ https:/myurl/$1 [NE,L,R=301
</VirtualHost>
I have this in my security.yaml:
access_control:
- { path: ^/, requires_channel: https}
In my framework.yaml:
trusted_proxies: '192.0.0.1,10.0.0.0/8, myurl'
# trust *all* "X-Forwarded-*" headers
trusted_headers: ['x-forwarded-for', 'x-forwarded-host', 'x-
forwarded-proto', 'x-forwarded-port', 'x-forwarded-prefix']
# or, if your proxy instead uses the "Forwarded" header
trusted_headers: ['forwarded']
I also created a certificate but I read somewhere that it's not a good way to do it with Symfony so I'm not using it. I did this because my error is: SSL_ERROR_RX_RECORD_TOO_LONG
And what I want is to redirect all HTTP to HTTPS and the:8000 to be automatically added.
Any ideas, please?
I use this in my httpd.config and it works:
<VirtualHost *:80>
ServerName olddomain.com
ServerAlias www.olddomain.com
Redirect permanent /FolderName/Filename_with_underscores.html http://newdomain.com/some-folder-with-dashes/?lang=fr
Redirect permanent /FolderName/Other_filename.html http://newdomain.com/some-other-folder/?lang=fr
Redirect permanent / http://newdomain.com/
</Virtualhost>
Now I'd like to put this in the .htaccess file in the root of the website.
I have tried several things but it keeps failing.
Does anyone know how to translate this httpd redirect to .htaccess?
There are capitals, dashes and underscores in the url's...
The code below works (but it's only half the story: it redirects all (www.)olddomain.com to the desired folder at newdomain.com):
RewriteCond %{HTTP_HOST} ^olddomain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http://newdomain.com/some-folder-with-dashes/?lang=fr" [R=301,L]
Thnx
You can use in your root .htaccess:
RewriteEngine on
# All rules only for olddomain.com
RewriteCond %{HTTP_HOST} !^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ - [L]
RewriteRule ^FolderName/Filename_with_underscores\.html$ http://newdomain.com/some-folder-with-dashes/?lang=fr [NC,R=301,L]
RewriteRule ^FolderName/Other_filename\.html$ http://newdomain.com/some-other-folder/?lang=fr [NC,R=301,L]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
I've searched for this and Im probably just sitting to close but I cant seem to get this rewrite to work. I have a file on a directory that I want to redirect to a vhost domain on the same server. Below is my condition, rule, and vhost
RewriteCond %{HTTP_HOST} ^/main$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R,l]
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /data/live/main
</VirtualHost>
If you want to redirect the olddomain folder main to the newdomain then you can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^main/(.*)$ http://newdomain.com/$1 [R=301,L]
If you want to redirect all the content of the olddomain to the newdomain then you can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
And if you want to remove the www from your newdomain:
RewriteCond %{HTTP_HOST} ^www\.newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Or you can do it directly on the virtualhost like this:
<VirtualHost *:80>
ServerName newdomain.com
Redirect permanent / http://www.newdomain.com
DocumentRoot /data/live/main
</VirtualHost>
I have a site with areas that require forced SSL mode and then forced to non-SSL for the rest.
I have started with the following rules:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/tourism/bookings/(.*) https://%{SERVER_NAME}/tourism/bookings/$1 [R,L]
RewriteRule ^/?bookings/(.*) https://%{SERVER_NAME}/bookings/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/bookings
RewriteCond %{REQUEST_URI} !^/tourism/bookings
RewriteRule (.*) http://%{SERVER_NAME}$1 [L,R,QSA]
</VirtualHost>
Now, the above works - but the SSL mode obviously interprets /includes/* to force it to non-SSL... is there an adjustment to the above to allow me to force ALL content except the two above (but any dependencies, like JS / CSS includes to follow the current protocol)?
Thanks in advance
If you want to exclude other things besides /bookings and /tourism/bookings then simply exclude them in conditions.
Also, you don't need the RewriteCond %{HTTPS} !=on condition since that virtual host is always going to be non-HTTPS:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule ^/tourism/bookings/(.*) https://%{SERVER_NAME}/tourism/bookings/$1 [R,L]
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule ^/?bookings/(.*) https://%{SERVER_NAME}/bookings/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/bookings
RewriteCond %{REQUEST_URI} !^/tourism/bookings
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule (.*) http://%{SERVER_NAME}$1 [L,R,QSA]
</VirtualHost>
I currently have some rules which work for directing http://domain.com to https://www.domain.com, but it doesn't work (I guess it's not matching?) for http://www.domain.com, which should redirect to https://www.domain.com. Could someone modify the below to do this? I've tried quite a few things but haven't been successful, this is my first time with .htaccess rewrite rules.
TL;DR, I need to redirect to both WWW and HTTPS
RewriteEngine On
RewriteRule .? - [E=PROTO:http]
RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{ENV:PROTO}://www.%{HTTP_HOST}/$1 [R=301,L]
EDIT: I've found the following code, which somewhat works - It redirects both to https://www.domain.com, but it gives a "Too many redirects" error.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ https://www\.%2%{REQUEST_URI} [L,R=301]
Have you tried something like this?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
It would be better to modify this in the VirtualHost for the server.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.domain.com
DocumentRoot /www/
SSLEngine On
# etc...
</VirtualHost>
This is the rule I'm using:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
You could include RewriteCond %{HTTPS} !=on as the first condition to force remove https if that's neccesary...
Hope that helps...