Lucas Scholten
Lucas Scholten

Reputation: 915

htaccess rewrite a single url with variable to another

I'm trying to rewrite

index.php/component/easydiscuss/tags?Itemid=1

to

easydiscuss1.htm

The rule I used is

RewriteEngine On
RewriteRule index.php/component/easydiscuss/tags?Itemid=1 easydiscuss1.htm [L]

However I just get a 404 for some reason even though this file is definitely on the server.

I think maybe the get parameters are causing a problem because I can rewrite urls without GET parameters like this fine.

Upvotes: 0

Views: 83

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Two things.

  1. The query string can't be matched against in a rewrite rule, so the ? in your regex pattern, is a regex ? quantifier, meaning the s right after the "tag" is optional. Obviously, that's not what you want at all.

  2. You are rewriting the URL to /easydiscuss1.htm, which means you will get a 404 unless the file easydiscuss1.htm is actually there. If it's there and you actually want to serve the file "easydiscuss1.htm" that's in your document root, then see #1 above.

Otherwise, you have the rule backwards. When you rewrite something, you take something the browser requests and you change that on the server. If you're changing it to something that's not there, then you should expect a 404 error. What you probably want is more along these lines:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php/component/easydiscuss/tags\?Itemid=([0-9]+)
RewriteRule ^ /easydiscuss%2.htm [L,R=301]

That will redirect the browser, telling it the resource that it requested is actually somwhere else. Then you need a rule to change the URL back:

RewriteRule ^/?easydiscuss([0-9]+)\.htm$ /index.php/component/easydiscuss/tags?Itemid=$1 [L]

EDIT

You need this:

RewriteCond %{QUERY_STRING} ^Itemid=1$
RewriteRule ^index.php/component/easydiscuss/tags$ easydiscuss1.htm [L]

Upvotes: 2

Related Questions