Solved

.htaccess directory rewrite

Posted on 2012-03-19
10
398 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Error Settings 12 80
apache vhosts on mac not being recognized 5 81
Missing Apache mod_DBD packages in Centos 7 2 76
.htaccess file settings 4 57
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 …
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now