Pls how do i make pretty url through .htaccess (i'm stocked)

I have been struggling with .htaccess file to make the URL’s of my site pretty for the past 2 days but nothing seems to work for me. I have watch many videos and follow tutorials still the problem is not solve, bellow are the problem am facing:
The site: have the following links:
/index.php, /about.php, /contact.php

Problem 1:
For index, about & contact.php I could easily use this. RewriteRule ^home /index.php rule to rewrite eg. domain/index.php to domain/home
What I aspect after this was to see domain/home at the address bar whenever I load the domain but it still shows domain/index.php but when I load domain/home it also works as index.php.
To solve this issue, I need a Rule that will be rewriting domain/index.php to domain/home permanently without having to load domain/home manually or differently.

Problem 2:

Issue 1:
Category.php use id to differentiate the categories available. Since /category.php?Id=1 display the category with the title of News, I want to rewrite /category.php?Id=1 to category/News and when the Id=2 it display the category with the title of Entertainment, so I need the rule to be applied to all id’s without having to rewrite rule for every id.

Issue 2:
Single.php display every single post from the site. Google was able to index single.php but not the links that hold the id. e.g /single.php?Id=2 display a post with the title “Benefits of livig in this world”. Single.php hold the links to all this post that are differentiated with id but Google was not able to index even one of this post links.
So, if /single.php?Id=2 display a post with the title “Benefits of livig in this world”. I need a rule that will rewrite and display /single.php?Id=2 permanently to either:
/Blogs/each-post-title/ (recommended)
/category-title/each-post-title/ (highly recommended)
And also it should be applied to all post id 's available under single.php and for the id of any post that will be posted later in single.php.

The last issue here is, Sitemap generators crawl only the index.php page. post links are not crawled. Google was not able to index the site domain: for almost 2 months now and when I submit sitemap of this site Google console said it’s HTML but my other domains from other providers have same site files with this site and are all indexed except for the single.php links with id’s. I don’t know what is blocking Google from indexing this site.

Those problems can be solved with the following:

  1. Add this code to the top of your .htaccess file. It will remove the .HTML and .PHP file extensions.
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]

RewriteRule ^ %1 [R,L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]

RewriteRule ^ %1 [R,L,NC]
  1. Use Cloudflare with your site. This will remove the id, and allow sitemap generators to view your site.

Hi thank you for your support:
Pls will cloudflare do the following:

  1. Allow Google to index
  2. Index multiple dynamic links differentiated By id’s in single.php
  1. Yes
  2. Not sure what you mean (If you talking about the ID that was added by IF, it will be removed.)

I wanted to rewrite post links from this single.php?Id=1 to this single/tittle-of-the-post for all post with id. but if you think google can still index all the post just by following the 2 solution you provided then everything is solved.

Now i’m a bit confused.

You links are like this: domain.tld/single?id=title-of-post
And you want Google to index them like this: domain.tld/single/title-of-post ???
I don’t think that will work no matter what you do.

With some clever .htaccess rules, you can map to But if you want to have the title in the URL, that’s not going to work. Because how would .htaccess know which post title is linked to which ID?

So what almost every CMS and framework does, is they have a few very simple .htaccess rules to send all traffic to a central index.php file, and handle the routing in PHP code.

This can be very simple: just explode the $_SERVER['REQUEST_URI'] parameter, see if the first part is Blogs and then look up the actual blog post from the second part in the database.

But there are also fancy libraries available you can use that can do a lot more, like this one:

Which method works best for you depends on your needs and experience. It’s a bit too much information for a forum post.

If you want pretty URLs, you’re going to need some .htaccess rules. But that doesn’t mean that everything must be implement as .htaccess rules. PHP is a lot more powerful than .htaccess rules.

1 Like

ok, i understand clearly now. thank you for all your support.

Your site doesnt work

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.