lovephp Posted August 4, 2017 Share Posted August 4, 2017 (edited) guys i got this code RewriteBase /site/ RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ post.php?id=$1 [QSA,L] RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L] the post.php works ok but then the cat.php redirects back to index.php and if i remove from the htaccess the post.php part RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ post.php?id=$1 [QSA,L] then the cat.php code works ok on the browser what is wrong in these codes please help out someone to make my urls work. thanks Edited August 4, 2017 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/ Share on other sites More sharing options...
Sepodati Posted August 4, 2017 Share Posted August 4, 2017 Both rules have the same pattern. Only one can be matched. Wouldn't you only need the conditions once, too? Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549273 Share on other sites More sharing options...
lovephp Posted August 4, 2017 Author Share Posted August 4, 2017 Both rules have the same pattern. Only one can be matched. Wouldn't you only need the conditions once, too? Please help out would appreciate. Been trying all day but just no success Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549274 Share on other sites More sharing options...
lovephp Posted August 4, 2017 Author Share Posted August 4, 2017 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549279 Share on other sites More sharing options...
lovephp Posted August 4, 2017 Author Share Posted August 4, 2017 (edited) doing so also does not work instead i get page not found error RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ cat.php?cat=$1 [QSA] RewriteRule ^(.*)$ post.php?id=$1 [L] but if i remove the line RewriteRule ^(.*)$ post.php?id=$1 [L] then the category works and if i remove the RewriteRule ^(.*)$ cat.php?cat=$1 [QSA] then the post works but both together i get page not found error Edited August 4, 2017 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549284 Share on other sites More sharing options...
Sepodati Posted August 4, 2017 Share Posted August 4, 2017 Do you know what a pattern is? Do you know what those symbols mean or are you just copy-pasting? I'm no expert in this area, but having the same pattern twice has to confuse the process. You are saying "if you have an A in your name, turn left" and then in the next line saying "if you have an A in your name, turn right". Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549293 Share on other sites More sharing options...
requinix Posted August 4, 2017 Share Posted August 4, 2017 As has been stated repeatedly, you cannot use both of those Rules because they both apply to the case when a URL does not exist. Let's just forget all this and start from the beginning. What URLs are you trying to rewrite to post.php and to cat.php? Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549294 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 As has been stated repeatedly, you cannot use both of those Rules because they both apply to the case when a URL does not exist. Let's just forget all this and start from the beginning. What URLs are you trying to rewrite to post.php and to cat.php? Yes page i got two pages which fetching the url from database one is for category that is cat.php and another one is post.php the query seems to be working ok but only issue is with htaccess now only one works at a time if add another then either i get page not found for entire dir or the link just redirects back to index.php. I got no idea how i can make this work anymore :-( Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549296 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 Do you know what a pattern is? Do you know what those symbols mean or are you just copy-pasting? I'm no expert in this area, but having the same pattern twice has to confuse the process. You are saying "if you have an A in your name, turn left" and then in the next line saying "if you have an A in your name, turn right". Honestly speaking mate htaccess is not my field so im having a really tough time. Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549297 Share on other sites More sharing options...
Sepodati Posted August 5, 2017 Share Posted August 5, 2017 (edited) Why do you think you need .htaccess files and rewrite rules? Why not just send the data directly to post.php or cat.php? Edited August 5, 2017 by Sepodati Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549299 Share on other sites More sharing options...
requinix Posted August 5, 2017 Share Posted August 5, 2017 Okay, maybe I wasn't clear. What URLs are you trying to rewrite to post.php and to cat.php? What are some actual URLs you are trying to rewrite? Not a description. Actual examples. Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549301 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 Okay, maybe I wasn't clear. What are some actual URLs you are trying to rewrite? Not a description. Actual examples. Ok site url looks like http://site.com/categoryname http://site.com/post-title Im using localhost but it should looks like this in real too. Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549302 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 Why do you think you need .htaccess files and rewrite rules? Why not just send the data directly to post.php or cat.php? Like how mate? Well this code does work just that when use for multiple its conflicting Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549303 Share on other sites More sharing options...
requinix Posted August 5, 2017 Share Posted August 5, 2017 Ok site url looks like http://site.com/categoryname http://site.com/post-title I recommend not doing that: there's no separation between categories and posts. At the very least you should put categories under a "subdirectory" like /category/categoryname. http://site.com/category/categoryname http://site.com/post-titleIf the majority of your site revolves around posts then you can leave it at the top (/post-title) but if you have many other pages that are not posts then you should give them a directory prefix too (/post/post-title). Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549305 Share on other sites More sharing options...
Jacques1 Posted August 5, 2017 Share Posted August 5, 2017 Turn your brain on, dude. You cannot use the same URL pattern for categories and posts. It's not possible. No SEO expert can do it. Not even Chuck Norris can do it. It's nonsensical. Let's say I had this URL: https://mysite.com/paris Tell me: Is that a category or a post? You don't know, right? Well, then how on earth is Apache supposed to know? Apache has to be able to distinguish between categories and posts. You need two different URL patterns. For example, posts could be subresources of categories: https://yoursite.com/some-category/some-post Or you could have two entirely separate paths: https://yoursite.com/categories/some-category https://yoursite.com/posts/some-post Whatever you do, the URLs of categories and paths must be distinguishable. 1 Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549306 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 I recommend not doing that: there's no separation between categories and posts. At the very least you should put categories under a "subdirectory" like /category/categoryname. http://site.com/category/categoryname http://site.com/post-titleIf the majority of your site revolves around posts then you can leave it at the top (/post-title) but if you have many other pages that are not posts then you should give them a directory prefix too (/post/post-title). yes exactly how do make the category like category/categoryname? my site mostly depends on post so i believe can let the site.com/post-title as it is but only category i need to fix now. Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549312 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 Turn your brain on, dude. You cannot use the same URL pattern for categories and posts. It's not possible. No SEO expert can do it. Not even Chuck Norris can do it. It's nonsensical. Let's say I had this URL: https://mysite.com/paris Tell me: Is that a category or a post? You don't know, right? Well, then how on earth is Apache supposed to know? Apache has to be able to distinguish between categories and posts. You need two different URL patterns. For example, posts could be subresources of categories: https://yoursite.com/some-category/some-post Or you could have two entirely separate paths: https://yoursite.com/categories/some-category https://yoursite.com/posts/some-post Whatever you do, the URLs of categories and paths must be distinguishable. totally agree and yes i realize now the issue but how do i change the htaccess to make it work that way? do i even have to change coding in php? Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549313 Share on other sites More sharing options...
requinix Posted August 5, 2017 Share Posted August 5, 2017 Here's what you had to start with. RewriteBase /site/ RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ post.php?id=$1 [QSA,L] RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]1. Since categories are going to start with "/category", you have to edit the stuff that matches categories to suit. Try it. 2. Rules are executed in order. It's okay if two rewriting rules can technically match the same URL, but you have to make sure that the first one to match is always the one that is supposed to match. "/category/category-name" will match both the post rules (not a directory, not a file, and matches the RewriteRule) and the category rules (not a directory, not a file, and matches the RewriteRule you've just fixed), so you need to move them around so that the category one matches first. If it still doesn't work after you address the two things above, post what you have. Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549318 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 i tried this RewriteBase /site/ RewriteCond %{REQUEST_FILENAME} !-d [NC] RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^(.*)$ post.php?id=$1 [QSA,L] RewriteRule ^category/(\d+)*$ cat.php?cat=$1 and here is the cat.php codes $stmt = $db->prepare('SELECT catID, catName, catUrl FROM blog_categories WHERE catUrl = :catUrl'); $stmt->execute(array(':catUrl' => $_GET['cat'])); $row = $stmt->fetch(); //if post does not exists redirect user. if($row['catUrl'] == ''){ header('Location: ./'); exit; } i tried to access it as http://localhost/site/category/name and i get error "The page isn’t redirecting properly". Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549319 Share on other sites More sharing options...
requinix Posted August 5, 2017 Share Posted August 5, 2017 1. RewriteRule ^category/(\d+)*$ cat.php?cat=$1That is incorrect. Drop the unnecessary (and problematic) * from it. But you know that only matches numeric categories, right? Because \d is a digit which means \d+ is a number. "name" won't match. 2. Redirecting to ./ will send the user to /category. Are you sure that's what you want? Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549320 Share on other sites More sharing options...
lovephp Posted August 5, 2017 Author Share Posted August 5, 2017 1. RewriteRule ^category/(\d+)*$ cat.php?cat=$1That is incorrect. Drop the unnecessary (and problematic) * from it. But you know that only matches numeric categories, right? Because \d is a digit which means \d+ is a number. "name" won't match. 2. Redirecting to ./ will send the user to /category. Are you sure that's what you want? no thats not what i want all i want is to be able to display site.com/category and posts related to that category name but guess its just too tough to achieve it. 3 days i been trying but but knowledge in htaccess is less so tried asking friends even they could not solve this Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549321 Share on other sites More sharing options...
Solution requinix Posted August 5, 2017 Solution Share Posted August 5, 2017 It's not too tough. As I see it, the main problem is that you know just enough to venture out and try doing everything, but you don't know what you actually need to know. 1. If you want actual category names then use .* instead of \d+ 2. If you want /category to show, presumably, all categories, then fix your cat.php so that if "cat" is empty then it shows all categories instead of redirecting. Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549323 Share on other sites More sharing options...
lovephp Posted August 9, 2017 Author Share Posted August 9, 2017 1. RewriteRule ^category/(\d+)*$ cat.php?cat=$1That is incorrect. Drop the unnecessary (and problematic) * from it. But you know that only matches numeric categories, right? Because \d is a digit which means \d+ is a number. "name" won't match. 2. Redirecting to ./ will send the user to /category. Are you sure that's what you want? did like u said mate and now problem solved RewriteRule ^post/(.*)\?*$ post.php?$1 [L,QSA] RewriteRule ^category/(.*)\?*$ categories.php?$1 [L,QSA] RewriteRule ^tag/(.*)\?*$ tag.php?$1 [L,QSA] thanks aton Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549495 Share on other sites More sharing options...
lovephp Posted August 9, 2017 Author Share Posted August 9, 2017 Turn your brain on, dude. You cannot use the same URL pattern for categories and posts. It's not possible. No SEO expert can do it. Not even Chuck Norris can do it. It's nonsensical. Let's say I had this URL: https://mysite.com/paris Tell me: Is that a category or a post? You don't know, right? Well, then how on earth is Apache supposed to know? Apache has to be able to distinguish between categories and posts. You need two different URL patterns. For example, posts could be subresources of categories: https://yoursite.com/some-category/some-post Or you could have two entirely separate paths: https://yoursite.com/categories/some-category https://yoursite.com/posts/some-post Whatever you do, the URLs of categories and paths must be distinguishable. ok i did it like u guys told me to its fixed now Quote Link to comment https://forums.phpfreaks.com/topic/304504-experts-please-take-a-look-seo-url-issue/#findComment-1549496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.