David R.
David R.

Reputation: 684

htaccess redirects are confusing

I have a link like this:

http://www.example.com/blog/something/article-name/

I want to re-write it to:

http://www.example.com/article-name/

the "something" is variable so I can't just use RedirectMatch 301 ^/blog/something/(.*)$ http://www.example.com/$1

I've searched every article I could find with various htaccess redirects and none applied to my scenerio, that is 1 fixed folder (blog) and 1 variable folder (something -- could be anything). I've tried this and it doesn't work either:

RedirectMatch ^/blog/(.*)/(.*)$ http://www.example.com/$2

.... just gives me the same thing minus blog.

Please help!

Upvotes: 0

Views: 44

Answers (1)

Mike Rockétt
Mike Rockétt

Reputation: 9007

Your question is somewhat unclear, because you refer to rewriting and redirecting. In the case of rewriting, Panama Jack is right. If you are looking for the shorter URI for the purposes of rewriting, the server cannot simply guess the something part.

However, if you are trying to redirect (say, you have changed your CMS, for example), then you can use the following. The mod_rewrite rule below will redirect /blog/variable/article to /article with a 302 temporary redirect.

RewriteEngine On
RewriteRule ^blog/([^/]+)/([^/]+)/?$ /$2 [R=302,L]

If you would like to make the redirect permanent and cached by your browser, change 302 to 301.

Upvotes: 1

Related Questions