Reputation: 6499
I've seen questions regarding redirecting a URL with a query string to a new URL that doesn't contain a query string.
Unfortunately I have to do this in reverse, my knowledge of redirecting in Apache isn't good, and I can't find any information about doing this.
So for example I need to redirect something like:
/news/news-item
to:
/news?item=news-item
The new URL structure is obviously not ideal, but this is something that is out of my control.
I've tried:
RedirectMatch 301 /news/news-item http://www.example.com/news?item=news-item
But obviously this doesn't work.
From what I understand I need to use RewriteRule
, can somebody point me in the right direction?
Upvotes: 0
Views: 2088
Reputation: 602
I'm probably to late with my answer, but this is how I would have done it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/(.*) /news?item=$1 [R=301,L]
</IfModule>
I would also use RewriteCond %{REQUEST_FILENAME} !-f
and RewriteCond %{REQUEST_FILENAME} !-d
. In case a file or a directory with the specified name exists, it would not procede with the rewrite rule below.
Upvotes: 2
Reputation: 6499
I managed to figure this out after a bit of research.
I'm not sure if this is 'ideal' way of doing this, but it seems to work well from my perspective.
Instead of using a Redirect
of a RedirectMatch
it's possible to achieve this using RewriteRule
, something like:
RewriteRule ^news/?(.*) http://example.com/?posts=list [R=301,L]
This will redirect any request to news
or news/x
(where x could be anything) to:
http://example.com/?posts=list
I should have made it clearer in my initial question that the query string didn't need to be generated using part of the old URL, if you need to do this I think you'll need to look into something like this:
How to redirect URLs based on query string?
Upvotes: 0