0

I am trying to pass a variable from a URL to PHP using mod_rewrite while maintaining standard WordPress functionality.

Here's my mod_rewrite rules:

RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/$ /index.php?state=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

When I go to this URL: http://epiclasers.com/arizona/

It is obviously being handled by index.php (because the theme is being loaded) and the state variable is being passed to PHP. but I am having trouble getting the home page content to be displayed - instead it is showing 404 "page not found" content.

Can someone shed some light as to why this is happening?

UPDATE:

When I redirect using the R flag, it works as it should:

RewriteRule ^(\w+)/$ /index.php?state=$1 [R,L]

and the home page is served: http://epiclasers.com/?state=arizona

It seems that WP is grabbing the URL using $_SERVER['REQUEST_URI'] to process requests/queries - and WP is getting confused by the /arizona/ in the URL?

Let's just assume that mod_rewrite is functioning correctly. Does anyone know how to customize WordPress to ignore "/arizona/" in the URL?

2 Answers 2

1

I suppose you've forgotten the QSA directive, to add properly the query string:

RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/$ /index.php?state=$1 [QSA,L]
RewriteRule ^index\.php$ - [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [QSA,L]

Tell me if it works, and if not, I'll do my best to answer.

Two hints:


Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

10
  • Hi Oliver. I tried that, but it didn't change anything. I am still getting a 404. The only query I am passing is the "state" one, and that is going through fine. I'm thinking this has something to do with the way WP processes requests/queries directly from the URL?
    – meeble
    Dec 4, 2011 at 18:12
  • 1
    I've added my two usual hints. May I ask you to add the tracelog to your question as well? Dec 4, 2011 at 18:35
  • Newer versions of mod_rewrite apparently use "LogLevel". I get internal server errors when I try to use any of those Mod_rewrite logging directives. this is from my error log: /home/epiclasers/epiclasers.com/.htaccess: LogLevel not allowed here
    – meeble
    Dec 4, 2011 at 21:58
  • In your global config, make sure you have the directive for this directory "AllowOverride All". Then if you have "Options +FollowSymLinks" in your htaccess, comment it. Then restart your server. Note: this is temporary, only to use rewritelog file, don't forget to go back to the original state once you're done with the problem. Dec 5, 2011 at 5:58
  • global config? This is just a virtual host - the only way I can configure anything is via .htaccess files. I already spoke with my web host and they do not allow mod_rewrite logging for virtual host clients. Again, I don't think there is any problem with mod_rewrite - it seems to be working exactly as it should.
    – meeble
    Dec 5, 2011 at 7:51
0

Ok, I ended up figuring this out. Like I thought, the problem had nothing to do with mod_rewrite and everything to do with WordPress.

To solve the issue, I had to use the following code:

if(isset($_GET['state'])) {
$url = explode("/", $_SERVER['REQUEST_URI']);
$url = end($url);
$_SERVER['REQUEST_URI'] = "/" . $url;
}

This code in essence removes the "/arizona/" from the REQUEST_URI global and replaces it with "/", which then forces WP to serve up the front page template from the index.php file. Fixed!

Perhaps a few of you can vote this answer up for me. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.