I'm stuck with this, my skills in the web servers area are poor...
I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:
=== /etc/nginx/sites-enabled/example.com ===:
http://dpaste.com/82638/
=== /etc/apache2/sites-enabled/example.com ===:
http://dpaste.com/hold/82645/
=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:
http://dpaste.com/82643/
In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That worked perfectly.
But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:
http://example.com/example.wsgi/
Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:
rewrite ^/(.*) http://example.com/$1 permanent;
but now I am getting what firefox calls a "circular loop"...
So who can I get this (I guess trivial) thing up?
Thanks in advance,
Hector
The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com
server {
listen 80;
server_name www.example.com;
rewrute ^/(.*) http://example.com/$1 permanent;
}
All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.
Related
I have to redirect url in .htaccess:
original: /mobile/blog/
redirect : /blog/
I tried:
RewriteCond %{REQUEST_URI} ^/mobile/blog/
RewriteRule ^mobile/blog/(.*)$ /blog/$1 [R=301,L]
Without success.
I aslo have these nginx redirect that need to be in .htaccess.
rewrite ^/items/(.+)/(.*) /search/$2 permanent;
rewrite ^/items/(.*) /search/$1 permanent;
rewrite ^/topic/onlyon $scheme://$host/tags/onlyon permanent;
Any help and documentation is appreciated.
Since Drupal sometimes updates the .htaccess file and Drupal often maintains a set of redirect internally it often makes sense to handle redirects using the Redirect module.
That said, if you need or want to do redirects in the .htaccess you can do that just fine. The first redirect you can do without rewrite, you can use Apache's redirect directive:
Redirect permanent /oldlocation /newlocation
To move everything in a directory using rewrite you can use something like this:
RewriteRule ^items(/.*)?$ /search/$1
See also: Apache rewrite rule for whole directory
I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]
I am looking to migrate from old domain to new domain.
I have my old domain olddomain.com and new domain newdomain.com pointing to same ip address for now.
I have Apache server inplace to handle requests.
How do I 301 redirect all my
olddomain.com/*
&
www.olddomain.com/*
to
newdomain.com/*
Can I get exact regex or configuration that I need to add in htaccess.
My newdomain.com and olddomain.com both are being serverd by same apache from same IP so "/" redirect might lead to cycles? And so was looking for effecient way
I tried
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
# RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>
And even tried adding in virtual host
RedirectMatch (.*)\.jpg$ http://comp17$1.jpg
But it does not redirect site when i hit localhost in browser to my computer name i.e comp16
In the configuration (VirtualHost) for each of your olddomain.com host try this:
Redirect permanent / http://newdomain.com/
Apache documentation for Redirect. This is the preferred way when everything should be redirected. If you must use mode_rewrite/htaccess there are plenty of questions around this on SO and one of them is:
How do I 301 redirect one domain to the other if the first has a folder path
EDIT
Recommendation from Apache regarding simple redirects:
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of
URLs, to somewhere else, should be accomplished using these directives rather than
RewriteRule. RedirectMatch allows you to include a regular expression in your
redirection criteria, providing many of the benefits of using RewriteRule.
I also recommend to use an If statement as you can use it also in a multisite server. Just type:
<If "%{HTTP_HOST} == 'old.example.com'">
Redirect "/" "https://new.example.com/"
</If>
Write the below code in to your .htaccess and it will redirect all your old domain request to new domain.
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
I've got myself an apache server and I'm trying to learn it's quirks...
I've got DNS to point example.com to 1.2.3.4 and I've set up a vhost on the apache2 server.
Now I want to, in the same vhost, point db.example.com to serv the contents of example.com/db.
I was hoping I could use some kind of mod_rewrite og ServerAlias, but sofar I've hot a wall.
The DNS part is covered, I just can't figure out what to do in apache.
In your .htaccess file, try something among the lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^db\.example\.com
RewriteRule (.*) http://example.com/db/$1 [L]
I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.