Getting PHP to read the clean URL created by .htaccess

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acs8
    New Member
    • Mar 2010
    • 1

    Getting PHP to read the clean URL created by .htaccess

    Hi there, I wonder if you can help me out.

    I'm trying to create clean URLs and so far I've managed to use .htaccess to convert the following URL:



    into:



    The problem is then getting PHP to interpret the URL and extract the variables. Here's the code I'm using:

    .htaccess code:
    Code:
    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine on
    
    #INTERNAL STATIC URL TO DYNAMIC URL
    RewriteRule ^/([^/]+)/?$ /index.php?c=$1 [L]
    RewriteRule ^/([^/]+)/([^/]+)/?$ /index.php?c=$1&p=$2 [L]
    
    #EXTERNAL DYNAMIC URL TO STATIC URL
    RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]{1,3}\ /index\.php\?catagory=([^&]+)\ HTTP/
    RewriteRule ^index\.php$ http://www.example.com/%1/? [R=301,L] 
    RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]{1,3}\ /index\.php\?catagroy=([^&]+)&page=([^&]+)\ HTTP/
    RewriteRule ^index\.php$ http://www.example.com/%1/%2/? [R=301,L]
    PHP code:
    Code:
    $urlVariables = explode("/",$_SERVER['REQUEST_URI']);
    $catagory = $urlVariables[1];
    $page = $urlVariables[2];
    With this I keep getting a 404 error page. If I add index.php into this line of the htacces code:
    Code:
    RewriteRule ^index\.php$ http://www.example.com/[B]index.php[/B]/%1/? [R=301,L]
    The page at least loads, but the css and images are not loaded.

    Any suggestions to what I'm missing here?
    Many thanks
    Last edited by Dormilich; Mar 9 '10, 09:41 PM. Reason: Please use [code] tags when posting code
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You need to make an exception for js and css files I think.

    Not sure.

    Comment

    • philipwayne
      New Member
      • Mar 2010
      • 50

      #3
      Someone correct me if I'm wrong but I think that could be due to paths. Your no longer at a relative path you need to use absolute paths if your using rewrite.


      /home/stylesheets/main.css != /home/rewriten/stylesheets/main.css

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hey.

        To fix the stuff like images and css files, you could try something like:
        [code=htaccess]RewriteCond %{REQUEST_URI} \.(css|js|jpe?g |png|gif|ttf|eo t)$ [NC]
        RewriteRule ([^/\.]+)\.(css|js|jpe ?g|png|gif|ttf| eot)$ /resources/$2/$1.$2[/code]
        That should rewrite all of the listed extensions into "/resources/ext/file.ext".
        (Note I haven't tested it yet.)

        It is of course best to just use absolute paths when loading resources.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          As to your internal rewrites, I see one potentially fatal problem.
          Your first rule, "^/([^/]+)/?$", matches all requests that start with a "/", that include an undetermined amount of non "/" characters, and that may or may not end in "/"... That'll match pretty much everything, including "/index.php?c=xxx &p=yyy".

          This will most likely leave you in an indefinite loop, eventually causing Apache to break out with a 500 code. (A match causes an internal redirect, which is then process again as a new request and subsequently passed through mod_rewrite again.)

          Also, I'm not entirly sure you should be including the "/" char at the beginning of the regular expression. Apache may strip it from the request URL. (Not 100% sure though.)

          Try something like this instead:
          [code=htaccess]RewriteRule ^([^/]+)/?([^/]+)?/? index.php?c=$1& p=$2 [L,QSA][/code]
          (Note, this one replaced both of your internal requests. The second option is made optional by the ? char.)

          P.S.
          If you need to specify some specific directory before the "index.php" , you may want to try the RewriteBase instead.
          [code=htaccess]RewriteBase http://example.com/some/dir/
          RewriteRule ^([^/]+)/?([^/]+)?/? index.php?c=$1& p=$2 [L,QSA][/code]
          There the index.php in the RewriteRule is rewritten as http://example.com/some/dir/index.php?c=xxx&p=yyy

          Comment

          Working...