Solved

.htaccess directory rewrite

Posted on 2012-03-19
10
399 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 6

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 6

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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