Apache rewrite rule only working when rewriting root - ruby-on-rails

I have an apache server running on localhost:8888. The following Virtual Host rule points to my rails application on localhost:3000:
<VirtualHost *:*>
ProxyPreserveHost On
RewriteEngine on
RewriteRule "^/(.*)$" "http://localhost:3000/$1" [P]
ProxyPass "/ia" "http://localhost:3000"
ProxyPassReverse "/ia" "http://localhost:3000"
ServerName localhost:3000
</VirtualHost>
This works fine but the re-write rule is sending all requests to the rails server and I only want it to rewrite requests following http://localhost:8888/ia/*. I have tried:
RewriteRule "^/ia/(.*)$" "http://localhost:3000/$1" [P]
I have also tried:
RewriteRule "^\/ia\/(.*)$" "http://localhost:3000/$1" [P]
but this doesn't seem to re-write anything. Essentially, without the rewrite rule, if I go to http://localhost:8888/ia none of the images/css will render because it's not re-writing links to localhost:3000. What am I missing?

Related

Facebook shared link does not open in FB iOS App in-app browser

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!

creating wildcard based subdomain [duplicate]

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.

Tell Rails to ignore particular URL and let Apache handle

I have a rails app running on my Apache server via Passenger.
Occassionally I am using some PHP scripts for the website, and have placed them in the public directory.
When I go to /php/ I want Apache to handle the request with the PHP parser and have rails ignore it.
Currently I can go to /php/index.php and it works fine. However I need /php/ to work as well but rails keeps looking for the controller to handle it.
I have a feeling this is something to do with apache rewrite rules, but I cant figure it out.
I have used
RewriteEngine On
RewriteRule ^php - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
in the /php/.htaccess file but this doesnt work. I still get the page not found error by Rails.
I think you can do this in your Passenger app's main Apache config file - something along the lines of this
<Directory "/.../myapp/php">
PassengerEnabled off
AllowOverride all
</Directory>
inside your VirtualHost block should do the trick (of turning Rails off, at least - turning PHP on is up to you!).
My answer is based off the ModRails Apache documentation - see section 5.6 for more on the PassengerEnabled command.

How can I force 'www' subdomain w/ Apache2 + Rails + Phusion Passenger?

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.

Remove WWW prefix from your website

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

Resources