Home  >  Q&A  >  body text

htaccess does not allow access to subdirectories with parameters

I'm trying to create htaccess for seo urls, I created htaccess all urls are working fine excluding subfolders with parameters, they redirect to the site folder.

sitefolder is my root directory and all my files are there.

Example: The URL should look like this:

http://localhost/sitefolder/subcat/39/web-hosting
  http://localhost/sitefolder/profile/1/username

In htaccess it looks like this:

RewriteRule ^subcat/(.*)/([a-zA-Z0-9_-]+)$ subcat.php?q=&slug= [L,NC]
 RewriteRule ^profile/(.*)/([a-zA-Z0-9_-]+)$ show_profile.php?q=&slug= [L,NC]

The first parameter is id and the second parameter is slug, but neither of these two urls will go to url.

This is all my htacces code:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^contact-us contact-us.php [L,NC]
RewriteRule ^login login.php [L,NC]
RewriteRule ^register register.php [L,NC]
RewriteRule ^search search.php [L,NC]
RewriteRule ^about-us about-us.php [L,NC]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ detail.php?q=&slug= [L,NC]
RewriteRule ^cats categories.php [L,NC]
RewriteRule ^subcat/(.*)/([a-zA-Z0-9_-]+)$ subcat.php?q=&slug= [L,NC]
RewriteRule ^profile/(.*)/([a-zA-Z0-9_-]+)$ show_profile.php?q=&slug= [L,NC]

This is how I get the parameters in subcat.php I don't redirect to the index on error so I don't think it's because of php :

if(!empty($_GET['slug']) AND !empty($_GET['q'])) {
    $slug = $_GET['slug'];
    $pid = is_numeric($_GET['q']);
    $userQuery = $handler->getPosts("SELECT * FROM categories WHERE catId = ?", [$pid])->fetchAll();
   if($userQuery) {
      foreach($userQuery as $user){
         $catid = intval($user['catId']);
         $cat_title = htmlspecialchars($user['catName']);
         $cat_seo_url = htmlspecialchars($user['cat_seo_url']);
         $cat_descriptions = htmlspecialchars($user['cat_desc']);
         $cat_keywords = htmlspecialchars($user['cat_keys']);
         $cat_created = htmlspecialchars($user['cat_created_at']);
         $cat_img = htmlspecialchars($user['cat_img']);
      }

   }else{
      echo "Something went wrong while execute query.";
      exit();
   }
}else{
   echo "Something went wrong with parameters";
   exit();
}

P粉785905797P粉785905797226 days ago415

reply all(1)I'll reply

  • P粉450079266

    P粉4500792662024-03-31 14:53:01

    If subcat.php is called (as you stated in your comment), then .htaccess is not the problem. But there is no redirect to index.php (or any kind of redirect) in any of the code you posted.

    However, there seems to be a runtime error with this line:

    $pid = is_numeric($_GET['q']);

    is_numeric() just tests if the argument is numeric and returns a boolean, it does not return the numeric value of the variable, which is what you would expect. Therefore, your SELECT will always fail (or return the first item) regardless of the value of q.

    You might need something like this to retrieve the actual value of a specified URL parameter:

    $pid = is_numeric($_GET['q']) ? (int)$_GET['q'] : 0;

    But you need to first verify that these URL parameters (i.e. $_GET variables) are passed, otherwise your script will issue an E_NOTICE message.


    renew:

    Conflict with your RewriteRule directive:

    When you request http://localhost/sitefolder/subcat/38/web-design it will be caught by the first rule here which rewrites the request to detail.php? q=subcat/39&slug=web-design instead of subcat.php as you expected. Could this fail because q=subcat/39 might not be valid (I guess triggering a redirect to /sitefolder/)? < /p>

    You need to change the order of these rules and/or make the regular expression more specific to avoid conflicts. For example. If q is supposed to be a numeric ID, only match the numeric value (\d ), not literally anything (.*< /code>), that's what you're currently matching.

    You should also include end-of-string anchors in regular expressions, otherwise these rules will also match rewritten URLs and cause unnecessary loops. For example:

    RewriteRule ^contact-us$ contact-us.php [L,NC]

    I would also question the use of the NC flag here, as it might be passed by allowing contact-us and CONTACT-US< 来创建重复内容问题/code> (and everything in between content) access the same resources.

    Summary

    In other words, your rule should look more like this:

    RewriteRule ^contact-us$ contact-us.php [L]
    RewriteRule ^login$ login.php [L]
    RewriteRule ^register$ register.php [L]
    RewriteRule ^search$ search.php [L]
    RewriteRule ^about-us$ about-us.php [L]
    RewriteRule ^cats$ categories.php [L]
    RewriteRule ^(\d+)/([\w-]+)$ detail.php?q=&slug= [L]
    RewriteRule ^subcat/(\d+)/([\w-]+)$ subcat.php?q=&slug= [L]
    RewriteRule ^profile/(\d+)/([\w-]+)$ show_profile.php?q=&slug= [L]

    (\w is the shorthand character class for [a-zA-Z0-9_].)

    By making the regular expression more specific, you can avoid conflicts. These instructions are executed from top to bottom. The first matching rule wins.

    reply
    0
  • Cancelreply