user1592380
user1592380

Reputation: 36227

301redirect to remove folder from URL

I have:

mydomain.com/folder-name/segment1/segment2

I want to change it to:

mydomain.com/segment1/segment2

using a 301 redirect.

I've tried:

RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$  /test/$1 [L]

but its not working

here is my htacess file:

# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3

RewriteEngine on
RewriteBase /


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

Upvotes: 0

Views: 92

Answers (2)

anubhava
anubhava

Reputation: 784998

Simply change your code to:

RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]

Upvotes: 1

AbsoluteƵERØ
AbsoluteƵERØ

Reputation: 7870

The answer for the first part of the question should be like this:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]

The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:

RewriteCond %{REQUEST_URI} !^/test/.*$

This line says take everything and rewrite it to the /test/ directory:

RewriteRule ^(.*)$  /test/$1 [L]

So together anything that's not in the test directory is being rewritten to the test directory.

If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).

RewriteCond %{REQUEST_URI} ^/?test

You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:

RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]

Upvotes: 1

Related Questions