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(); }
If
subcat.php
is called (as you stated in your comment), then.htaccess
is not the problem. But there is no redirect toindex.php
(or any kind of redirect) in any of the code you posted.However, there seems to be a runtime error with this line:
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 ofq
.You might need something like this to retrieve the actual value of a specified URL parameter:
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 todetail.php? q=subcat/39&slug=web-design
instead ofsubcat.php
as you expected. Could this fail becauseq=subcat/39
might not be valid (I guess triggering a redirect to/sitefolder/
)?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 (.*), 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:
I would also question the use of the
NC
flag here, as it might be passed by allowingcontact-us
andCONTACT-US (and everything in between content) access the same resources.
Summary
In other words, your rule should look more like this:
(
\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.