Yanipan
Yanipan

Reputation: 664

Issue with specific 301 redirection in htaccess

I'm having an issue with a format of URL.

I need to send /en to /

The problem is that I need only /en (exactly), not /en/foo...

I know how to do it with string that ends with .html, but here I have other URLS that have /en/stuff and they are also matched.

Would really appreciate assistance...

Upvotes: 1

Views: 38

Answers (2)

Tom
Tom

Reputation: 26819

You need to specify end of string in RewriteCond e.g.

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/en$
RewriteRule ^(.*)$  / [L,R=301]

Please note that Condition pattern as well as Rewrite Rule pattern are a perl compatible regular expression (with some additions):

For Anchors:

  • ^ - Start-of-line anchor
  • $ - End-of-line anchor

EDIT

BTW, there is very nice htaccess tester at http://htaccess.madewithlove.be/ which you could use to test the rules.

Upvotes: 1

anubhava
anubhava

Reputation: 784938

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^en/?$ / [L,R=301,NC]

This will redirect /en or /en/ to / also note that /EN will also be handled because of NC (Ignore Case) flag used here.

Upvotes: 0

Related Questions