Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

.htaccess directory rewrite

Posted on 2012-03-19
10
Medium Priority
?
406 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month7 days, 13 hours left to enroll

824 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