Solved

.htaccess directory rewrite

Posted on 2012-03-19
10
396 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

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…
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

12 Experts available now in Live!

Get 1:1 Help Now