Hi I was trying to rewrite the URL of my page to 127.0.0.1/news/1/the-world-needs-you
from news.php?id=1&title=The World Needs You
IDK How to do it with htacces and I'm using mysql database to store information and show it on page.
Here is my .htaccess
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule news/[0-9]+/[.*].html news.php?id=$1&tile=$2
This is how you do it.
Options +FollowSymLinks
RewriteEngine on
RewriteRule news/(.*)/(.*)/ news.php?id=$1&title=$2
Pay attention to the two (.*)
as they map to $1 and then $2 based on the parameters.
Rewritten URL should look like this:
http://localhost/news/1/The World Needs You/
Edit :
To replace spaces with hyphens. The easiest way to do so is to :
1 ) Update the rule
RewriteRule news/(.*)/(.*) news.php?id=$1&title=$2
2 ) Access the url with
/news/1/The-World-Needs-You
3 ) If you're planning to display the title in your page. Replace it back using PHP.
?php
$title = $_GET['title'];
$r = str_replace("-"," ",$title);
?
EDIT 2 :
To answer your third question from the comments
thanks for the help. I have one more question. For example here in StachOverflow if you change the url a little bit it will redirect to the correct. how do yo do it – BrxDgr8t 1 hour ago
We first need to take a look at a sample request here in Stack overflow
http://stackoverflow.com/questions/41355986/url-rewrite-with-text/41356337?noredirect=1#comment69946939_41356337
In this url , besides all the extra information that is present, the key to it is the id that is present after /questions/
, in this case 41355986
. This number is the unique identifier for this question , and if they were using some type of SQL database , the request to get this question would be :
Select * from questions where id = 41355986
After that , it does not really matter what title or URL you visit because the unique identifier is the ID. Therefore any of the following examples will point to this post as long as the id {41355986}
is the same
http://stackoverflow.com/questions/41355986/url-rewrite-with-text
http://stackoverflow.com/questions/41355986/new-fake-url
Now , if you decide to alter any of the numbers from the question id , you will be redirected to a new question :
http://stackoverflow.com/questions/41355981/url-rewrite-with-text