neomang
neomang

Reputation: 149

HTAccess Rewrite Rules with get parameters

I am building a page that should receive get parameters to determine what content to display. In order to create better-looking URLs, I am using htaccess to allow the urls to appear as following:

http://www.domain.com/elections/candidate-speech/candidate-name/

and redirect to the page

http://www.domain.com/elections/candidate-speech.php?name=candidate-name

I have done this before successfully and was re-using rules that I wrote for a different page but with a similar desired structure. The rule I am using is:

RewriteEngine On    # Turn on Rewriting Engine

RewriteRule  ^candidate-speech/([A-Za-z0-9-]+)/?$    candidate-speech.php?name=$1

If i directly load the intended URL (http://www.domain.com/elections/candidate-speech.php?name=candidate-name) with a candidate's name, I get the page displayed as intended. When I attempt to use the shortened version, however, I get the correct page but nothing is provided in $_GET.

The .htaccess file is stored in the elections directory, as is the relevant .php file, which is the same setup I used in the past where this rule was successful. I am fairly new to htaccess, which is why I am falling back on my previous success with it. I have restarted the server, cleared my cache, et al. to no avail. What could I be doing wrong?

Upvotes: 1

Views: 654

Answers (1)

anubhava
anubhava

Reputation: 784958

Have it this way:

Options -MultiViews
RewriteEngine On
RewriteBase /elections/

RewriteRule ^candidate-speech/([\w-]+)/?$ candidate-speech.php?name=$1 [L,QSA,NC]

Disabling MultiViews option is important here since your .php file name is same as the starting component candidate-speech/.

Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

Upvotes: 1

Related Questions