开发者

Can't access $_GET after rewrite?

开发者 https://www.devze.com 2023-03-29 04:37 出处:网络
Here\'s the mod_rewrite portion of my .htaccess Options +FollowSymlinks Options -Indexes # Prevent Direct Access to files

Here's the mod_rewrite portion of my .htaccess

Options +FollowSymlinks

Options -Indexes

# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini)">
 Order deny,allow
 Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
RewriteBase /mysite/

# Protects file paths
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/index.php?route=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /mysite/index.php?route=$1&page=$2 [NC,QSA,L]

</IfModule>

When I type in http://localhost/mysite/home it correctly takes me to index.php?route=home.

When I type in http://localhost/mysite/services/ it works again, and I can correctly access $_GET['route']

so for example, my menu has:

<li<?php if($_GET['route'] == 'home') { echo ' class="selected"'; ?>>

to set the class on that page.

but....

When I type in http://localhost/mysite/services/widget, it correctly takes me to index.php?route=home&page=widget but I get a PHP error if I try to reference $_GET['page'] (or ANY $_GET value for that matter) ...which as I mentioned before, I was using to set the selected class of the menu item.

Can someone help me fix this? I must've done something wrong with the second rewriterule... because it doesn't pick up any $_GET values.

...

This is really strange because I use $_GET['route'] and $_GET['page'] to populate the correct开发者_如何转开发 html for the page (similar to an MVC setup)... I would've thought the php include I have on index.php would've failed if it couldn't find the $_GET

....

Edit:

Maybe it's relevant, but my index.php code looks like this:

if( isset( $_GET[ 'route' ] )) {
    if( file_exists( 'content/' . $_GET[ 'route' ] . '.php' )) {
      if( isset( $_GET[ 'page' ] )) {
        if( file_exists( 'content/' . $_GET[ 'route' ] . '/' . $_GET[ 'page'] . '.php' )) {
          include 'content/' . $_GET[ 'route' ] . '/' . $_GET[ 'page'] . '.php';
        }
      } else {
        include 'content/' . $_GET[ 'route' ] . '.php'; 
      }
    } else {
      header( "Location:404" );
      exit;
    }
  } else {
    header( "Location:home" );
    exit;
  }


I think your order of rewrite rule is a problem here. You should always have most specific rules first and then put generic ones. If you change your .htaccess like this:

RewriteBase /mysite

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /mysite/index.php?route=$1&page=$2 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/index.php?route=$1 [NC,QSA,L]

And make sure you have above code in mysite/.htacess file.

With above if you visit http://localhost/mysite/services/widget and do print_r($_GET); you will get:

Array
(
    [route] => services
    [page] => widget
)


Your rewrite rules are fighting each other; they're both set up to act on the exact same conditions. I'm no mod_rewrite expert, but that seems like a clear problem to me.


That's strange. If your GET vars are present in the URL PHP should be able to grab them. Try stacking your rewrites like this and see if it helps:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?route=$1&page=$2 [NC,QSA,L]
RewriteRule ^(.*)$ index.php?route=$1 [NC,QSA,L]
0

精彩评论

暂无评论...
验证码 换一张
取 消