mod_rewrite in httpd.conf not working (but does in .htaccess) - apache

I have a very similar setup on 2 different servers (the working one is a Plesk install on Centos 6, the other is pretty plain LAMP setup on Centos 6) and im having an issue with mod_rewrite on the plain LAMP server.
I have compared the httpd.conf (and associated includes) on both servers and both are (for all intents and purposes) the same.
On the plesk server i have the following in my httpd.conf file which redirects http://stats.domain.com to http://www.domain.com/webstat/ and this works a treat;
<VirtualHost *:80>
ServerName stats
ServerAlias stats.*
UseCanonicalName Off
RewriteEngine On
RewriteCond %{HTTP_HOST} stats\.(.*) [NC]
RewriteRule ^.*$ http://www.%1/webstat/ [L,R=301]
</VirtualHost>
On the LAMP server i have the following in my httpd.conf file which is supposed to redirect http://stats.domain.com to http://www.domain.com/awstats/awstats.pl?config=domain.com
<VirtualHost *:80>
ServerName stats
ServerAlias stats.*
UseCanonicalName Off
RewriteEngine On
RewriteCond %{HTTP_HOST} stats\.(.*) [NC]
RewriteRule ^$ http://www.%1/awstats/awstats.pl?config=%1 [L,R=301]
</VirtualHost>
This is not working when in the httpd.conf file though. If i add stats.domain.com as virtualhost with full documentroot, etc and then add the same rewrite to .htaccess within that documentroot the redirect works fine.
If i enable rewrite logging then i can see the .htaccess version being worked through, but nothing from the httpd.conf. I cannot for the life of me work out what is wrong/missing :(

The rewrite rule for your LAMP server is just putting in domain.com.
RewriteCond %{HTTP_HOST} stats\.(.*) [NC]
RewriteRule ^$ http://%1/awstats/awstats.pl?config=%1 [L,R=301]
Your comment above says it should be www, more like the first rule.
RewriteRule ^.*$ http://www.%1/awstats/awstats.pl?config=%1 [L,R=301]

Related

Using .htaccess to redirect all requests to subdomain

I'm hosting a Laravel application on my server and have set up a subdomain to host it in my virtual host.
I have another subdomain on my server and after hours of playing around trying to set up an .htaccess file, I came up with the below which redirects all requests to www.mysite.net/example to my subdomain example.mysite.net (e.g www.mysite.net/example/12345 goes to example.mysite.net/12345)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mysite\.net)$ [NC]
RewriteRule ^(.*)$ http://example.%1/$1 [R=301,L,NE]
I'm wanting to tweak this to work with Laravel, but it doesn't quite work the same considering Laravel is hosted out of the following path mysite.net/laravel/public rather than mysite.net/example.
How would I edit the above .htaccess to redirect all requests to mysite.net/laravel/public to laravel.mysite.net? I.e mysite.net/laravel/public/12345 would redirect to laravel.mysite.net/12345
Edit
Here is the Virtual Host I have added through Apache
<VirtualHost *:80>
ServerName laravel.mysite.net
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public>
Options -Indexes
</Directory>
</VirtualHost>
Place this rule as your very first rule inside /var/www/laravel/public/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?mysite\.net$ [NC]
RewriteCond %{THE_REQUEST} /laravel/public/(\S*)\s [NC]
RewriteRule ^ http://laravel.mysite.net/%1 [L,R=302]
You can use redirect rule of htaccess to redirect any directory to a different domain. Use the below code:
Redirect 302 /laravel/public/ http://laravel.mysite.net/
Please let me know if it helps you out.

Apache server config redirect from IP to domain name EC2

I am running an apache webserver on a linux EC2 instance.
The problem is that you can access the server using the IP address, DNS and the domain name. This causes a problem for SEO and I want to tidy it up.
I have read on the apache documentation that you can do a mod_rewrite and this needs to be done in the httpd.conf if you have root access otherwise in the .htaccess for per directory override.
I have root access so I am trying to change the httpd.conf
If the user types in
http://52.17.12.123/
or
http://ec2-52.17.12.123.eu-west-1.compute.amazonaws.com/
I want them to be redirected to
www.example.com
This is what I tried
<VirtualHost *:80>
DocumentRoot "/var/www/html/my-website"
# Other directives here
RewriteEngine On
RewriteCond %{HTTP_HOST} !^52.17.12.123.com$
RewriteRule /* http://www.example.com/ [R]
</VirtualHost>
It seems to partially work but www.example.com does not load due to to many redirects.
--EDIT--
Thanks, so now in my httpd.conf I now have the following configuration
Listen 80
NameVirtualHost *:80
DocumentRoot "/var/www/html/my-website"
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)$ [NC]
RewriteRule ^/(.*)$ http://www.example.com [R=301,L]
It is all working correctly now
It seems to partially work.
I doubt, considering the rule you currently have in your httpd.conf.
You can have it this way
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

redirecting non existed page to home page and example.org to www.example.org in .htaccess with virtual hosting

I am testing out a few things on Ubuntu apache2. I am able to solve the individual problems but when those are combine it messed up and showing "this webpage has a redirect loop”. Okay..Here is the setup.
My site is www.example.org and subfolder www.example.org/kmc. In subfolder, we have Joomla CMS installed.
In .htaccess file, I did two setup.
1) example.org will be forwarded to www.example.org
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)S http://%{HTTP_HOST}/$1 [R=301,L]
It works.
2) the non-existed page will be forwarded to the home page.
ErrorDocument 404 http://www.example.org
It works too.
Now I setup the virtual host in the 000-default.conf file. The reason is I want two different sites on the same server.
<VirtualHost *:80>
DocumentRoot /var/www/html/kmc
ServerName www.example.org
</VirtualHost>
Now when I go to the example.org, the browser shows "This Webpage has a Redirect Loop".
You www rules should be like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Your rule is missing www. in the target URL.

magento site redirecting to http://www.hpslightshop.com/

Today I'm trying to configure the sub-domain in Apache web-server(Centos) using virtual-host.Sub domain pointing is working fine.
error
magneto site redirecting to different site (http://www.xxxx.com/)
Note :-
1.IN core_config_data table.I have changed the data to http://test.xxxx.com/
2.cleared /var/cache and /var/session folders in your magento root
<VirtualHost *.80>
DocumentRoot /var/www/html/
ServerName test.xxxx.com
ErrorLog /var/log/xxxx_error_log
CustomLog /var/log/xxx_access_log
</VirtualHost>
Add the below code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>
if you run
select * from core_config_data where path like '%base_url%' is changed or not

MOD REWRITE (BASE) on WAMP Server

I have an WAMP webserver with Rewrite module activated.
I have all my projects in:
d:/prj/costumer1/www/ (alias: costumer1)
d:/prj/costumer2/www/ (alias: costumer2)
and so on...
For costumer1 I have an .htaccess-file that works just fine. Looking like this:
RewriteEngine on
RewriteBase /costumer1/
RewriteRule ^([^/\.]+)/?$ index.php?a=$1 [QSA]
RewriteRule ^([^/\.]+)/?/([[a-zA-Z0-9_-]+)$ index.php?a=$1&b=$2 [QSA]
RewriteRule ^([^/\.]+)/?/([[a-zA-Z0-9_-]+)/?/([[a-zA-Z0-9_-]+)$ index.php?a=$1&b=$2&c=$3 [QSA]
Now when I create a src/href-link I now have to use:
/costumer1/search/book/novell (aka: costumer1/?a=search&b=book&c=novell)
instead of
/search/book/novell (aka: costumer1/?a=search&b=book&c=novell)
So in short:
I don't want to write "/costumer1" in front of every link: search
I have done something like this in my root .htaccess
RewriteCond %{HTTP_HOST} ^customer1$ [NC]
RewriteCond %{REQUEST_URI} !^/customer1
RewriteRule ^(.*)$ customer1/$1 [L,NS]
But eventually, I moved away from handling this with .htaccess as it was less scalable, and went on to setting virtual hosts, something like this:
<VirtualHost *:80>
DocumentRoot "D:/htdocs/customer1"
ServerName customer1
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/htdocs/customer2"
ServerName customer2
</VirtualHost>
And for this to work, of course edit your hosts file, to have lines like this:
127.0.0.1 customer1
127.0.0.1 customer2