Solved

.htaccess directory rewrite

Posted on 2012-03-19
10
400 Views
Last Modified: 2012-03-20
I am in the situation where I need to rewrite all pages within a directory (/categories/Sports) to another directory (/categories/Sports-Equipment). I have the following htaccess in place - which is created by the software - but no matter how many options I try I can't get it to work (anything that does work seem to result in a permanent redirect.

Anyone any ideas? It would also be useful if the code included a 301!

Options -MultiViews +FollowSymlinks -Indexes

# Force 401 pages to use a valid error document
# so they do not fall back to the 404 which is
# triggered by ISC. This allows HTTP Auth to work
# correctly
ErrorDocument 401 "Unauthorized access"

<IfModule mod_security.c>
	# Turn off mod_security filtering.
	SecFilterEngine Off

	# The below probably isn't needed, but better safe than sorry.
	SecFilterScanPOST Off
</IfModule>

<IfModule mod_rewrite.c>
	RewriteEngine On
	# If your server is running PHP in CGI mode you will probably need to uncomment the following lines
	# Only uncomment lines that begine with Rewrite

	# The RewriteBase should be set to the same value as the AppPath setting in your config.php WITH a / on the end
	# RewriteBase /

	# This rewrite base rule is only required if you are getting 401 unauthorized errors when placing an order with Google Checkout
	# RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
	#
	# Alternatively, if the above does not work, try uncommenting this line below instead:
	# SetEnvIfNoCase Authorization "Basic ([a-z0-9=]+)" REMOTE_AUTHORIZATION=$1

	RewriteCond %{REQUEST_FILENAME} robots.txt
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule robots.txt robots_default.txt [L]

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . index.php
	RewriteRule ^categories/Sports(.*)$ http://www.sportswarehouse.co.uk/categories/Sports-Equipment$1 [L,R=301]
	<IfModule mod_env.c>
		SetEnv SEO_SUPPORT 1
	</IfModule>
</IfModule>

<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml
</IfModule>

Open in new window

0
Comment
Question by:mcfinster
  • 4
  • 4
  • 2
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37740892
is your apache configured to use .htaccess files, i.e.
   Allowoverride All
0
 
LVL 7

Expert Comment

by:torakeshb
ID: 37740972
0
 

Author Comment

by:mcfinster
ID: 37741178
ahoffman,

Yes, the server is configured properly.

torakeshb,

Been through that url but it doesn't cover changing a directory name and also the url of any files in that directory!
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 7

Expert Comment

by:torakeshb
ID: 37741789
This should be the configuration for redirecting from folder to a folder.

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^categories/Sports(.*)$ http://www.sportswarehouse.co.uk/categories/Sports-Equipment$1 [L,R=301]

</IfModule>
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37741810
RewriteRule ^/*categories/Sports(.*)$ http://www.sportswarehouse.co.uk/categories/Sports-Equipment$1 [L,R=301]
0
 

Author Comment

by:mcfinster
ID: 37741814
Thanks for that, strange thing is (as I'd discovered before) the only place in the htaccess file I can get this to do anything is right at the top, but if placed there it results in a redirect loop. Anywhere else and it doesn't work. Not sure what's stopping it working!!
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 37741844
# oops, missed the condition:
RewriteCond %{REQUEST_FILENAME} !^/categories/Sports-Equipment
RewriteRule ^/*categories/Sports(.*)$ http://www.sportswarehouse.co.uk/categories/Sports-Equipment$1 [L,R=301]
0
 

Author Comment

by:mcfinster
ID: 37741878
Hmm,m, still get a redirect loop with that

Loop
http://www.sportswarehouse.co.uk/categories/Sports-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment-Equipment/ 

Open in new window



htaccess Code
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !^/categories/Sports-Equipment
RewriteRule ^/*categories/Sports(.*)$ http://www.sportswarehouse.co.uk/categories/Sports-Equipment$1 [L,R=301]

</IfModule>

Options -MultiViews +FollowSymlinks -Indexes

# Force 401 pages to use a valid error document
# so they do not fall back to the 404 which is
# triggered by ISC. This allows HTTP Auth to work
# correctly
ErrorDocument 401 "Unauthorized access"

<IfModule mod_security.c>
	# Turn off mod_security filtering.
	SecFilterEngine Off

	# The below probably isn't needed, but better safe than sorry.
	SecFilterScanPOST Off
</IfModule>

<IfModule mod_rewrite.c>
	RewriteEngine On
	# If your server is running PHP in CGI mode you will probably need to uncomment the following lines
	# Only uncomment lines that begine with Rewrite

	# The RewriteBase should be set to the same value as the AppPath setting in your config.php WITH a / on the end
	# RewriteBase /

	# This rewrite base rule is only required if you are getting 401 unauthorized errors when placing an order with Google Checkout
	# RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
	#
	# Alternatively, if the above does not work, try uncommenting this line below instead:
	# SetEnvIfNoCase Authorization "Basic ([a-z0-9=]+)" REMOTE_AUTHORIZATION=$1

	RewriteCond %{REQUEST_FILENAME} robots.txt
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule robots.txt robots_default.txt [L]

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . index.php
	Options +FollowSymLinks
	<IfModule mod_env.c>
		SetEnv SEO_SUPPORT 1
	</IfModule>
</IfModule>

<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml
</IfModule>

Redirect permanent /doe http://www.sportswarehouse.co.uk/categories/Outdoor/Duke-of-Edinburgh/

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37741972
do you need to take care of the text behind "/sports" ?
if not, we can simplify and strengthen the rules:

RewriteCond %{REQUEST_FILENAME} !^/categories/Sports-Equipment/*$
RewriteRule ^/*categories/Sports$ http://www.sportswarehouse.co.uk/categories/Sports-Equipment [L,R=301]
0
 

Author Comment

by:mcfinster
ID: 37742000
Ah, no it wasn't that but adding trailing slashes to all the links in the htaccess has works. Thanks for pointing me in the right direction!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question