This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Apache rewrite based on subdomain
I am stuck with one problem which not able to solve it. Plz help me to come out of this.
My requirement is:
I want to have each city name as sub domain in my site. Say www.mysite.com, which can have www.delhi.mysite.com, www.bangalore.mysite.com. The list might go endless. My problem is I don't want to create folders for each of the sub domains. I want to handle it in the URL as query string, say www.mysite.com?city=bangalore. This way I can redirect the request to a single file.
I have made the set up LAMP architecture.
In vhost file, it is
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster#localhost
ServerName www.mysite.com
DocumentRoot /var/www
</VirtualHost>
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster#localhost
ServerName *.mysite.com
DocumentRoot /var/www
</VirtualHost>
and Also updated .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.mysite\.com
RewriteRule ^(.*)$ http://www.mysite.com/index.html [R,L]
But still I am getting 404 error.
Is my approach correct?? Can it be achievable??
Please help me in doing this.
Try something along these lines:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
RewriteRule ^/(.*)$ http://www.domain.com/%1/$1 [L,R]
plus a rule to not apply this to the www prefix.
And next time, please use the search function, this has been asked before.
Make sure you have enabled the rewrite module. Try putting the statements into your vhost config instead of .htaccess.
Related
We have a website deployed in a Ubuntu 14 server running Apache. The website uses an SSL certificate (so you can access through https) and has a redirection for all the incoming requests without https.
Ok, so the website is working as expected in desktop browsers and mobile browsers, but, when users tap on the links (from the Facebook iOS app) the in-app browser tries to open the website and returns a message like "Link doesn't work, try again" (not the exact message in english).
After a lot of research I still don't have a clear idea of what this can happen (just found a couple of unresolved cases on google), so I'd start by looking at the server configuration.
The apache configuration (IP and domains are samples):
<VirtualHost 200.200.200.200:80>
ServerName www.domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost domain.com:80>
ServerName www.domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost www.domain.com:443>
ServerName www.domain.com
ServerAlias domain.com
SSLEngine on
SSLCertificateFile /home/ssl/www.domain.com.crt
SSLCertificateKeyFile /home/ssl/domain.com.key
ServerAdmin admin#domain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
The previous configuration works fine in all browsers, it just doesn't load (doesn't even makes it to Apache access log) when loaded with Facebook iOS in-app browser.
Help is much appreciated! Thanks in advance for your time.
After analyzing the url with the SSL Checker suggested by #CBoroe, I find out that there was a missing intermediate certificate configuration that needed to be added because we're using Apache < 2.4.8 version.
We just had to add the SSLCertificateChainFile /home/cert_ssl/intermediate.crt directive. The final SSL part of the code looks like this:
SSLEngine on
SSLCertificateFile /home/ssl/www.domain.com.crt
SSLCertificateKeyFile /home/ssl/domain.com.key
SSLCertificateChainFile /home/ssl/intermediate.crt
Hope this helps someone else having this issue. After this change, restart Apache with:
service apache2 restart
And everything should work fine (you can use the same SSL checker tool to validate it).
Thanks for your time!
I checked my site with online SEO checker and it suggested that I have to fix this error:
Your site's IP xxx.xxx.xx.xxx does not redirect to your site's domain name.
So as suggested I added some code to my htacces file located in public directory. Of course replacing x's with IP adress.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
But then after app restart I checked again with SEO checker and again it shows the same error.
Here is my full htacces file:
PassengerAppRoot /home3/ecotec11/rails_apps/darbs/
<IfModule mod_passenger.c>
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
#Set this to whatever environment you'll be running in
RailsEnv development
RackBaseURI /
SetEnv GEM_HOME /home3/ecotec11/ruby/gems/gems
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.darbs.ecotechno.lv$ [NC]
RewriteRule ^(.*)$ http://www.darbs.ecotechno.lv/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{HTTP_HOST} ^173\254\28\107
RewriteRule (.*) http://www.darbs.ecotechno.lv/$1 [R=301,L]
Any suggestions?
Thanks
(1) Your actual IP 173.254.28.107 points to a hosting service. The hosting service cannot point to your site because it probably hosts thousands of other sites.
A solution might be to buy a dedicated IP from that hosting service. Depending on their policies, you might (or might not) have access to your own document root, or your public html directory might be your document root.
(2) If this were a dedicated server or at least dedicated IP, then the answer would be that you edited .htaccess located in a wrong place.
Place an .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
in the default document root, the one where the index.html that says "There is no website configured at this address" is located. You can either look for a file named index.html or see what is the default document root in the Apache config file.
He People.
I have a rails app running for multiple sites and it has a cache that looks like this:
tmp/cache/adomain.com/the cached files
No this is not picked up by Apache (obviously) and i am trying to set it up
in my httpd.conf. But I wasn't able to get it working.
This is something i tried:
< VirtualHost *:80 >
PassengerMaxPoolSize 20
PassengerPoolIdleTime 0
DocumentRoot /mnt/app/current/public
RewriteEngine On
RewriteCond /mnt/app/current/tmp/cache%{HTTP_HOST}%{REQUEST_URI} -f
< /VirtualHost>
But it doesn't seem to work! (of course I restarted apache with: apache2ctl restart)
I googled a lot! but nowhere i did find a solution.
Looks to me like you're missing a RewriteRule declaration following your RewriteCond.
RewriteCond provides conditional matching of requests, but doesn't take action without a rule to do something.
Probably change to the following, your paths may vary:
RewriteCond /mnt/app/current/tmp/cache%{HTTP_HOST}%{REQUEST_URI} -f
RewriteRule ^/[^.]+$ /YOUR_CACHE_URI/%{REQUEST_URI} [QSA,L]
My clients want to use 301 redirects to force the 'www' subdomain on their sites. So 'example.com/something' resolves to 'www.example.com/somthing' etc.
What I'm trying to do is simply add this to my vhost file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName example.com
ServerAlias www.*
DocumentRoot /data/apps/example.com/current/public
RailsBaseURI /
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
I've also ensured that mod rewrite was enabled via:
sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload
The outcome of my current efforts basically appears to be successful. Apache restarts and everything works as expected EXCEPT the rewrite isn't happening. So both 'www.example.com' and 'example.com' resolve. The browser doesn't get redirected to 'www.example.com' when it should. Any ideas? I've tried reloading the configuration and restarting apache several times but I can't seem to get the rewrite rules working. Am I setting them up in the wrong place by placing them here in the vhost instead of a .htaccess file?
Any advice here would be useful. I'm completely baffled.
Put this below your main VirtualHost entry:
<VirtualHost *:80>
ServerName example.com
RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>
You main VirtualHost should have a ServerName www.example.com entry and no aliases or redirects. Also, mod_rewrite is not required for this redirect.
How does Stack Overflow (and other web sites) remove the 'www' prefix when it's entered as part of a URL?
Is it a redirect, a rewrite or something else entirely?
Update: I'd specifically like to know in the context of IIS 6
On Apache, it looks like this (inside an .htaccess file):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
An easy way to do this is using the Apache "Redirect" directive:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
# remainder of server configuration goes here
</VirtualHost>
The Redirect directive automatically preserves anything following the / in the URL. I find this method easier to read and understand than the Rewrite method.
Firing up Fiddler, we can see that the server responses with a "301 Moved Permanently" status and refers it to http://stackoverflow.com .
Since StackOverflow is hosted on Windows 2k8 IIS7 they set up this redirect straight away in IIS7.
FYI:
a list of HTTP statuses
If you are a .NET developer you might know "Respose.Redirect" , this creates a 302 Object Moved status. Search engines like 301 status codes in this case better, because they know they should not come back to www.stackoverflow.com in the future.
You can do it several ways, using mod_rewrite and redirecting is my favorite. Something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.cuenca.co$ [NC]
RewriteRule ^(.*)$ http://cuenca.co/$1 [R=301,L]
redirect. the sub-domain "www.stackoverflow.com" would simply redirect to "stackoverflow.com".
You need a default dns entry added pointing to your web server.
ping site.com and verify ip is pointing to webserver, if not you need to get the default DNS entry added.
for a basic setup:
You'll have to add host headers http://www.visualwin.com/host-header/
Create 1 site with a hostheader of www.site.com
In the Home Directory tab, set it to a permanent redirect to http://site.com
Create a 2nd site with a host header of site.com
If you want www.site.com/file.html to redirect to site.com/file.html you will need a more advanced setup with something like ISAPI_Rewrite or use custom 404 pages to do it.
You can do what mod_rewrite does for Apache, with a comparable URL rewriter for IIS. A good one is IIRF. The rule is:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [I]
RedirectRule ^(.*)$ http://example.com/$1 [R=301]
You can also wildcard the hostname like so:
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [I]
RedirectRule ^(.*)$ http://example.com/$1 [R=301]
IIRF is free to use.
For apache
<VirtualHost *:80>
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld