简体   繁体   中英

Mod-Rewrite and .htaccess Problems

Having some issues with Mod_Rewrite. I must admit this is my first venture into the module so I could be going through something silly. I have been trying to get this to work on my own for a day or so off and on and I can not figure out why this is not working.

I am getting the following errors:

Notice: Undefined index: Dir in D:\\wamp\\www\\WildForFashion\\Portal\\Index.php on line 31

What I am trying to accomplish is to be able to have one template page (index.php) and have Mod_Rewrite change the url for me so it can be Search Friendly and to also ease coding and design.

I was also having issues when it came to no Images / CSS files loading at all, I did read that by using an absolute url would work.

As of right now I am working in the PORTAL directory first (which is an admin page for website maintenance) and then will be moving onto the root directory. I am not sure if I need to have one .htaccess file or two (one being in root dir, second being in portal dir).

The DIR folder is to hold each category and page for the website like the following:

Domain.com/{CATAGORY}/{PAGE}/
ROOT / DIR / {CATAGORY} / {Page}.php

Domain.com/Portal/{CATAGORY}/{PAGE}/
ROOT / Portal / DIR / {CATAGORY} / {Page}.php

.htaccess Before

RewriteEngine on
RewriteRule ^/Portal/$      Portal/Index.php?Dir=Portal&Page=Home   [L,QSA]
RewriteRule ^Portal/(.*)/(.*)$  Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule ^Portal/(.*)/$      Portal/Index.php?Dir=$1&Page=Home   [L,QSA]

.htaccess After (Working w/ Exceptions)

RewriteEngine on
RewriteRule Portal/$     Portal/Index.php?Dir=Portal&Page=Home   [L,QSA]
RewriteRule Portal/([^/]+)/$     Portal/Index.php?Dir=$1&Page=Home   [L,QSA]
RewriteRule Portal/([^/]+)/([^/]+)/$     Portal/Index.php?Dir=$1&Page=$2 [L,QSA]

Index.php

<?php
include_once("DIR/" . $_GET['Dir'] . "/" . $_GET['Page'] . ".php");
?>

Directory Tree

  • CSS
  • DIR
  • IMG
  • JS
  • SRC
  • Portal
    • CSS
    • DIR
      • Portal
      • Inventory
      • Stats
      • Orders
    • IMG
    • JS
    • SRC
    • Index.php
    • .htaccess
  • Index.php

You are including forward slashes in the matching rule that don't need to be there and excluding them from the redirect path where they do need to be. Try this:

RewriteRule Portal/$      Portal/Index.php?Dir=Portal&Page=Home   [L,QSA]

RewriteRule Portal/(.*)/(.*)$  Portal/Index.php?Dir=$1&Page=$2 [L,QSA]

RewriteRule Portal/(.*)/$      Portal/Index.php?Dir=$1&Page=Home   [L,QSA]

Note I removed the ^ from the matching portions of the rewrite rules as this indicated taht should be the beginning of the URI. I also removed the "/" in front of the substritution pattern.

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