Redirect visitor with .htaccess - regex

I've got an e-shop on a virtual server that's been used as a subdirectory for the last few years, but now I'm finally giving the VS it's own domain name. What I really need is visitors to the old URL to be transparently (and 301) redirected to the new URL with everything after /eshop/ maintained and apended to the new host.
I.e. http://www.example.com/eshop/page.php -> http://www.newdomain.com/page.php
Any help would be greatly appreciated.

This should work with Apache:
RewriteEngine On
RewriteBase /
RewriteRule ^/eshop(/.*)? http://www.newdomain.com$1 [R=permanent,L]
This redirects http://www.example.com/eshop/whatever to http://www.newdomain.com/whatever and also redirects http://www.example.com/eshop to http://www.newdomain.com

You did not specify which web server you were using, but I assume its either apache or lighttpd.
In apache, you can use the Redirect keyword, e.g.
Redirect 301 / http://www.newdomain.com/
I haven't tried this, but see e.g. here:
http://www.yolinux.com/TUTORIALS/ApacheRedirect.html#APACHE
It seems to work with .htaccess files as well.
In lighttpd, there is mod_redirect (and I did try this :) ):
http://redmine.lighttpd.net/wiki/1/Docs:ModRedirect
EDIT:
Redirect 301 /eshop/ http://www.newdomain.com

Related

.htaccess redirect only working for part of the URL

I'm trying to redirect a bunch of URLs from an old site to the shiny relaunched site. My .htaccess is working with the first level of directories (i.e. example.com/directory is redirecting to example.com/newdirectory), but I'm having issues redirecting beneath that. Here's an example of one that's not working:
Redirect 301 /Expertise/QuantitativeResearch/ /services/quantitative-research/
This goes to example.com/services/QuantitativeResearch - so the Expertise directory is being redirected, but the QuantitativeResearch part of the URL isn't.
I've tried changing it to RedirectMatch301, but it doesn't help. I'm wondering if I need to strip out /Expertise/ and use some Regex instead? The /Expertise/ part is already being redirected to /services/ by another line:
Redirect 301 /Expertise/ /services/
Could this be causing conflict? I know I'm probably missing something really basic here! Just to clarify, my redirects are all coming after and before RewriteEngine On.
Thanks in advance!
You do need to use regex based matching here. Try RedirectMatch instead:
RedirectMatch 301 ^/Expertise/QuantitativeResearch/?$ /services/quantitative-research/
RedirectMatch 301 ^/Expertise/(.*)$ /services/$1

Insert a directory at the start of a URL in Apache Config

Apologies if this question has already been asked before. I couldn't find an example that handled my exact situation.
I have an Apache Server and have access to the httpd.conf file.
I have a domain (say www.example.com) and I want to insert a directory (say test) after the domain.
So for example I want www.example.com to be mapped to www.example.com/test and www.example.com/folder to be mapped to www.example.com/test/folder and so on.
I have achieved this using the a RedirectMatch directive like this:-
<VirtualHost *:80>
ServerName www.example.com
RedirectMatch ^/$ test/
</VirtualHost>
However this changes the URL in the browser to include the test folder and I would like to keep this hidden from the end user.
I have tried using a rewrite rule but my lack of regex knowledge has let me down here! This is what I have tried (within the virtual host element):-
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1
I have tried various combinations with this but nothing seems to work!
Any help would be much appreciated.
Ok, I found the problem here. This was working ok but my browser cache (Firefox) was remembering the old values.
I turned of caching in the browser (by going to about:config and setting browser.cache.disk.enable = FALSE
Then everything started working correctly.
Hopefully this will help others who have the same issue!

I need to find the proper 301 redirect

I'm having trouble with a 301 redirect. I've tried a few but they are not working as I need it.
I have an old website that no longer exists, I would like to forward the entire domain to a landing page on my new website.
So when someone types in www.olddomain.com I want them to go to www.newdomain.com/landingpage.php
The problem is that I want ALL requests on the old domain to go to that landing page. Currently I have the domain forwarded thru GoDaddy, and when someone (or Google) types in www.olddomain.com/whateverpage.htm or forwards it to www.newdomain.com/landingpage.php/whateverpage.htm
This is causing issues with Google indexing.
So I need a redirect that will send ALL requests of ANY page at www.olddomain.com/ to www.newdomain.com/landingpage.php
What should I use?
You can use this rule on DocumentRoot of old site:
RewriteEngine On
RewriteRule ^ http://newdomain.com/landingpage.php? [R=301,L]
Assuming you're using a LAMP stack (linux server, MySQL, PHP) you can use rewrite rules in an .htaccess file to capture all requests to www.newdomain.com/landingpage.php* and redirect them back to landingpage.php. If you're hosting on a Windows server, you can do these sort of redirect rules with the URL Rewrite module in IIS (which can import .htaccess rules).

How to enable SSL for a whole django site with apache2+ubuntu 11?

I need to enable SSL for one of my entire django site. Currently the site is hosted with Apache2 in Ubuntu 11.1 and just accessible through http. I'd like to know the following,
1) Apache configuration for enabling ssl for this site.
2) Django related changes of the same, if any.
Another question of the same kind is unanswered, so asking here again.
You may do it by adjusting your apache config like this:
# Turn on Rewriting
RewriteEngine on
# Apply this rule If request does not arrive on port 443
RewriteCond %{SERVER_PORT} !443
# RegEx to capture request, URL to send it to (tacking on the captured text, stored in $1), Redirect it, and Oh, I'm the last rule.
RewriteRule ^(.*)$ https://www.x.com/dir/$1 [R,L]
Note that this is taken from https://serverfault.com/questions/77831/how-to-force-ssl-https-on-apache-location.
There shouldnt be any changes necessary for django.
HTH.

Mod Rewrite rule to redirect all pdf request to another location

I'm trying to clean up our website. I'm using ISAPI Rewrite. I have figured out the regex I need to select the files in question.
^(?!.*?web_content.*?).*?\.pdf
I want to redirect all pdf requests to look in web_content/pdf/. So The pseudo rule I want is
redirect all requests to pdfs that aren't already being requested from
web_content/pdf. Drop off the original folder path.
/somefolder/this/mycool.pdf ==> /web_content/pdf/mycool.pdf
My question is how would I actually create the Mod rewrite rule? I don't know how to correctly do the replacement command. I also hope this rule wont affect external pdf links on our site.
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/web_content/pdf/
RewriteRule ^(.+\.pdf)$ /web_content/pdf/$1 [L]
Helicon ISAPI_Rewrite v3 is pretty much the same as Apache's mod_rewrite (except few advanced things), therefore almost all rules that work on Apache will work here as well.