How Do I redirect non-pretty Url to pretty Url pamanently

#Hello: in my localhost I have successfully rewrite this link news-details.php?nid=13 as news/13 using the following code:

RewriteEngine on
RewriteCond %{QUERY_STRING} !^nid=
RewriteRule ^news/(.+) news-details?nid=$1

For all the remaining non-pretty links both news-details.php?nid=idnum and news/idnum works perfectly without any error now but what is missing is how to ignore the non-pretty url and display the pretty url on the browser

I have tried 5 different htaccess code to redirect the non-pretty to the pretty url but no one succeeded

I have also tried changing the php code from:

From this: <.a href=“news-details.php?nid=<?php echo htmlentities($row['pid')?>” class=“btn btn-primary”> Read More →</a.>)

To this: <.a href=“news-details/<?php echo htmlentities($row['pid')?>” class=“btn btn-primary”> Read More →</a.>)

Then the nid in ($_GET[‘nid’]) bellow becomes undefined index

<?php $pid=intval($_GET['nid']); $query=mysqli_query($con,"select tblposts.PostTitle as posttitle,tblposts.PostImage,tblcategory.CategoryName as category,tblcategory.id as cid,tblsubcategory.Subcategory as subcategory,tblposts.PostDetails as postdetails,tblposts.PostingDate as postingdate,tblposts.PostUrl as url from tblposts left join tblcategory on tblcategory.id=tblposts.CategoryId left join tblsubcategory on tblsubcategory.SubCategoryId=tblposts.SubCategoryId where tblposts.id='$pid'"); while ($row=mysqli_fetch_array($query)) { ?>

The site got suspended so I use localhost, pls I need help To make this work using either php code or a simple htaccess code. Thank you

I think you’re looking for this:

RewriteEngine on
RewriteCond %{QUERY_STRING} !^nid=
RewriteRule ^news/(.+) news-details?nid=$1 [L,R=301]

Also, it is really hard to read your php script, so if in the future you could pretty-print it and put it in backticks (`) for one-line and 3 (```) for multiline. Read more about formatting here: https://commonmark.org/help/.

1 Like

@wackyblackie thanks for the reply. Am sorry that you found my php code confusing. What I Want to know about was, just Like the URL of this post which is How Do I redirect non-pretty Url to pretty Url pamanently

I need my browser to also display http://localhost/news/13 instead of http://localhost/news-detals?nid=13

But in the php code link its originally news-details.php?nid=13

Currently am using the codes you provide to rewrite news-detals?nid=13 to news/13 but it is not displaying news/13 on the browser ops! Except for the [L,R=301] flag, I haven’t added it but do you think it will work fine as I just explain by adding flag?

Try accessing it through news/13, if it works, add the [L,R=301] flag.

Yes, accessing it through news/13 is working

Its say access forbidden after ading the flag but without the flag it works but manually

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