jainchetan
jainchetan

Reputation: 96

.htaccess redirect one level if string exists in URL

Can anyone please point me to a tutorial or article or give a solution (with some explanation) on how to redirect one level up if certain string exists at the end of the URL.

For example

http://www.example.com/s/texas/hello-world/

should be redirected to

http://www.example.com/s/texas/

Assume the string is 'hello-world' and I have 100 different urls

i.e /s/florida/hello-world

/c/boston/massachusetts/hello-world etc.

Thanks in advance.

Upvotes: 2

Views: 282

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270637

The expression below captures everything before hello-world inside (.*) as $1 and redirects to it. The /? makes the trailing slash optional.

RewriteEngine On
RewriteRule ^(.*)hello-world/?$ $1 [L,R=301,QSA]

Upvotes: 1

Related Questions