Emre
Emre

Reputation: 57

.htaccess Redirect 301 with Rewriterule not working

When I try to redirect my page, nothing happens. If anyone tries to enter forum.example.com/forumdisplay.php?fid=1 I want them to be redirected to forum.example.com/forum-1.php. I am new to .htaccess and couldn't figure it out.

.htaccess file:

Options -MultiViews
RewriteEngine on
Redirect 301 /forumdisplay.php?fid=([0-9]+).php /forum-$1.php
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]

Any help will be appreciated.

Upvotes: 1

Views: 51

Answers (1)

MrWhite
MrWhite

Reputation: 45829

Redirect (mod_alias) and RewriteRule (mod_rewrite) belong to two different modules. You need to use mod_rewrite only for this. Try something like the following:

Options -MultiViews

RewriteEngine on

# Redirect direct requests for the "real" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^fid=([0-9]+)
RewriteRule ^forumdisplay\.php$ /forum-%1.php? [R=301,L]

# Internally rewrite back to the "real" URL
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]

(Redirect doesn't use regex either.)

UPDATE: I've appended a ? onto the end of the first RewriteRule substitution (ie. /forum-%1.php?). By specifying an empty query string, it removes the query string from the request. Alternatively, you can use the QSD flag on Apache 2.4+

Upvotes: 1

Related Questions