Mere Development
Mere Development

Reputation: 2519

How do I redirect a specific permalink wordpress page to another in .htaccess?

I have this .htaccess file as generated by Wordpress and an eCommerce plugin:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^.products/images/(\d+)/?\??(.*)$ /wp-content/plugins/shopp/core/image.php?siid=$1&$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

I now have to redirect someone who visits http://mydomain.com/golf to http://mydomain.com/products/category/golf/

I have these rules in a block above the existing block which semi-work:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^golf.*$ products/category/golf/ [R=permanent,L]
RewriteRule ^cycling.*$ products/category/cycling/ [R=permanent,L]
#...etc
</IfModule>

The problem is, if someone types http://mydomain.com/golfshoes for example, it still redirects. I have tried to play about with the wildcards etc but cant get it to only allow /golf and /golf/

Also... is that the most efficient way of setting these rules up? I should I be doing some sort of if or if or if then redirect?

Thanks :) Ben

Upvotes: 1

Views: 758

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Try changing the 2 regular expressions to:

RewriteRule ^golf(/.*)?$ products/category/golf/$1 [R=permanent,L]
RewriteRule ^cycling(/.*)?$ products/category/cycling/$1 [R=permanent,L]

The (/.*) matches a "/" or "/something", and the ? means that it may not be there. The $1 at the target indicates whatever was matched in the parentheses. So someone going to http://mydomain.com/golf/foo.html will get served /prodducts/category/golf/foo.html

Upvotes: 2

Related Questions