Mod_rewrite Question - .htaccess

I have an HTML file I want rewritten as a subfolder on the server.
http://www.example.com/kids-and-family/185-summer-camp.html
to be shortened to:
http://www.example.com/camp
Is there an rewrite condition where I can make this happen in .htaccess?
Can I say if (/camp) then display /kids-and-family/185-summer-camp.html?
I have been looking for this but have not found anything.

Try this rule:
RewriteEngine on
RewriteRule ^camp$ kids-and-family/185-summer-camp.html [L]
If you want an external redirect, add the R flag ([L,R]) with an additional redirect status like 301 for a permanent redirect ([L,R=301]).

in your .htaccess file, try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} "/camp"
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{DOCUMENT_ROOT} /kids-and-family/185-summer-camp.html -s
RewriteRule ^$ /kids-and-family/185-summer-camp.html [L]

Related

Redirecting to a different directory

I have a full site setup at mysite.com/2/
I want urls like:
mysite.com/about to redirect to mysite.com/2/about
mysite.com/work/photos to redirect to mysite.com/2/work/photos
I was hoping I could solve this and add the /2 after the domain through the htaccess file and not have to move the whole site up a level.
Try the folowing code if you want redirect
RewriteEngine On
RewriteCond %{REQUEST_URI} !(about|work/photos)/(2)
RewriteRule ^(about|work/photos)/(.*)$ /$1/2/$2 [R=301,L]
If you need only internal redirection change the last line with this :
RewriteRule ^(about|work/photos)/(.*)$ /$1/2/$2 [L]
If you want to change all requests :
RewriteEngine On
RewriteCond %{REQUEST_URI} !/(2)
RewriteRule ^(.*)/(.*)$ /$1/2/$2 [R=301,L]

301 redirect specific URLS if ?noRedirect not in URL

I'm running a wordpress installation and want to move specific feeds off-site. I've already got most of the technology down, but here's the problem. I want the following URL:
http://www.csicon.net/g/feed
to redirect to
http://feed.mesr.it/g
But if the URL comes in like this:
http://www.csicon.net/g/feed?noRedirect
I don't want it to redirect but load the original. Any thoughts?
The .htaccess file on http://www.csicon.net/ would contain:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\/g\/feed$ http://feed.mesr.it/g [L,R=301]
Note: It has not been tested, but you get the idea.
Later Edit: Also, this can be done in PHP.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)csicon\.net$ [NC]
RewriteCond %{QUERY_STRING} !(^|&)noRedirect [NC]
RewriteRule ^([^/]+)/feed/?$ http://feed.mesr.it/$1 [L,NC,R=302]

How do i redirect a directory to the parent directory in htacess

I have this website http://www.foo.com/bar/index.php. I recently moved my site FROM foo.com/bar/index.php TO foo.com/index.php
In my htaccess file, how do i write the conditions so that if any request is made from lets say .foo.com/bar/somedirectory/somefile REDIRECT to foo.com/somedirectory/somefile
In the htaccess file in your document root, preferably above any rules that do routing (to index.php, for example), add these rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/bar/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^/?bar/(.*)$ /$1 [L]
If you want to externally redirect the client (changing the URL in the browser's address bar), then add the R flag in the rule's brackets: [L,R=301].
Add below to your foo.com/bar/.htaccess file:
RewriteEngine On
RewriteRule ^(.*)?$ http://%{HTTP_HOST}/$1 [R=301,QSA,L]

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Using mod_rewrite and mod_alias (redirect 301) together in .htaccess?

I have a site with a set of old .html and .php pages that have been put into a CMS.
Currently in the .htaccess file there are about 30 mod_alias redirects in the following form:
redirect 301 /oldpage1.html http://www.example.com/newpage1.php
redirect 301 /oldpage2.php http://www.example.com/newpage2.php
redirect 301 /oldpage3.php http://www.example.com/newpage3.php
But we want to use mod_rewrite to have pretty URLs in our CMS, which will take the form http://www.example.com/pagename.php, so also have the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1
At the moment both are being applied together, which results in:
http://www.example.com/newpage1.php?page=oldpage1.html
How can I apply the rewrite rule only when no match has been made by the mod_alias redirect 301 statements, so that the following occurs:
http://www.example.com/oldpage1.html -> redirects to ->
http://www.example.com/newpage1.php -> which is treated as ->
http://www.example.com/index.php?page=/newpage1.php
Any hints would be very much appreciated? Thanks.
I found the answer in a great explanation of mod_rewrite and mod_alias
The problem is that mod_rewrite always occurs before mod_alias, regardless of the order the are placed in .htaccess. This is the reverse of the order required for this situation.
The trick is to use RewriteRule [R=301] instead of redirect 301, and hence use mod_rewrite for everything instead of mixing it with mod_alias.
Full solution is as follows:
RewriteEngine on
RewriteBase /
RewriteRule ^oldpage1.html /newpage1.php [R=301,L]
RewriteRule ^oldpage2.php /newpage2.php [R=301,L]
RewriteRule ^oldpage3.php /newpage3.php [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1

Resources