简体   繁体   中英

Mod_Rewrite inside directory not matching correctly

So I've got a root folder with this as it's .htaccess (it's a WordPress build):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

and I've got a subdirectory called safetytraining-2 inside it. Requests to the sub directory work fine, and I've got a file in safetytraining-2 called true.php and it's contents are this:

<html>
   <body>
      <?php var_dump($_GET);
      ?>
   </body>
</html>

Now inside safetytraining-2, I've got a .htaccess that I'm trying to test mod_rewrite with. Here's it's contents:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond  %{THE_REQUEST}      ^true\.php(.*)$
   RewriteRule  ^true\.php(.*)$     -                   [L]

   RewriteRule  ^(.*)$          true.php?p=$1       [L]
</IfModule>

Now here's the problem. When I go to website.com/safetytraining-2/foobar I get this for output:

array(1) { ["p"]=> string(8) "true.php" }

It's really confusing the crap out of me. I put in the first rule with the condition to try and stop apache from recursively applying the rewrite rule, but I'm guessing it didn't help. No matter what I put after /safetytraining/, I keep getting this output. Any help here guys? You can view the page at athanclark.com/safetytraining-2. Any help would be awesome!!

I just figured it out. Make sure you check your Apache version!!! Versions 1.x don't recognize Perl-compatible regular expressions, but rather use POSIX Extended regular expressions. Rewriting the .* s above to [A-Za-z0-9/=-_']+ works for me. I'll have to read up on POSIX-style ones more to see how to accept periods (.), but it works for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM