Home > Software design >  Htaccess doesn't let access to sub directories with parameters
Htaccess doesn't let access to sub directories with parameters

Time:04-29

I am trying to create a htaccess for seo urls, I created my htaccess all urls works fine exclude subfolder with parameters, they redirects to sitefolder.

sitefolder is my root all files are in there.

Example : Url should look like this :

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

in htaccess its like this :

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

First parameter is id and second is slug, but both urls doesn't go to url.

Here is all my htacces codes :

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=$1&slug=$2 [L,NC]
RewriteRule ^cats categories.php [L,NC]
RewriteRule ^subcat/(.*)/([a-zA-Z0-9_-] )$ subcat.php?q=$1&slug=$2 [L,NC]
RewriteRule ^profile/(.*)/([a-zA-Z0-9_-] )$ show_profile.php?q=$1&slug=$2 [L,NC]

And this is how I Get parameters in subcat.php, I don't redirect to index on error so I dont think its 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();
}

CodePudding user response:

If subcat.php is being called (as you appear to have stated in comments) then it's not a problem with .htaccess. But there are no redirects to index.php in any of the code you posted.

However, there would seem to be an error with this line:

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

is_numeric() simply tests whether the argument is numeric and returns a bool, it's not returning the numeric value of that variable, which is what you would seem to be expecting. Consequently, your SELECT is always going to fail (or return the first item), regardless of the value of q.

You perhaps need something like this instead:

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

But you need to be validating that these URL parameters (ie. $_GET vars) are passed in the first place, otherwise, your script is going to emit E_NOTICE messages.


UPDATE:

There is a conflict with your RewriteRule directives:

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

When you request http://localhost/sitefolder/subcat/38/web-design it's going to be caught by the first rule here which rewrites the request to detail.php?q=subcat/39&slug=web-design, not subcat.php as you are expecting.

You need to change the order of these rules and/or make the regex more specific to avoid a conflict. eg. If q should be a numeric id then match just a numeric value (\d ), instead of literally anything (.*), which is what you are currently matching.

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

You should also include end-of-string anchors on your regex, otherwise these rules will also match the rewritten URL and cause an unnecessary loop. For example:

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

I would also question the use of the NC flag here, since it's potentially creating a duplicate content issue by allowing contact-us and CONTACT-US (and everything between) to access the same resource.

Summary

In other words, your rules should be 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=$1&slug=$2 [L]
RewriteRule ^subcat/(\d )/([\w-] )$ subcat.php?q=$1&slug=$2 [L]
RewriteRule ^profile/(\d )/([\w-] )$ show_profile.php?q=$1&slug=$2 [L]

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

By making the regex more specific you avoid a conflict. The directives execute top to bottom. The first rule that matches wins.

  • Related