Hack Forums
Help with URL Rewriting - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Webmasters (https://hackforums.net/forumdisplay.php?fid=141)
+---- Forum: Website Construction (https://hackforums.net/forumdisplay.php?fid=50)
+---- Thread: Help with URL Rewriting (/showthread.php?tid=5321406)



Help with URL Rewriting - Mr. Man - Jun 28, 2016

PHP Code

<?php 
RewriteEngine On
RewriteRule 
^/book.php?name=(.+)$ /book/1$ [NC,R=301] 

When I enter this, www.mysite.com/book/mybook its going to www.mysite.com/book?name=mybook. What I want to do is, I want the url in the address bar to remain www.mysite.com/book/mybook.

Tried using R=301 but url in address bar doesn't seem to change. Any idea how to do this ?


* - Squito - Jun 29, 2016

You got the order wrong in RewriteRule.
PHP Code

<?php 
RewriteEngine On
RewriteRule 
^/book/(.*)$ /book.php?name=$1 [NC] 



* - Mr. Man - Jun 29, 2016

(Jun 29, 2016 12:23 AM)Squito Wrote:  You got the order wrong in RewriteRule.
PHP Code

<?php 
RewriteEngine On
RewriteRule 
^/book/(.*)$ /book.php?name=$1 [NC] 

With this, when i type mysite.com/book/bookname I get 404 error.


* - Squito - Jun 29, 2016

Is book.php in the root? Isn't there a typo in the filename of the PHP file?


* - Mr. Man - Jun 29, 2016

Yeah its in index. book.php?name=bookname fetches the book details. So when i type mysite.com/book/name it must still fetch it, donno what is wrong o.O

Checked cPanel error log, it says [Wed Jun 29 05:52:54 2016] [error] File does not exist: /home/****/public_html/book not found.


* - Squito - Jun 29, 2016

Check the apache error.log, it's usually more descriptive, than a usual 404 error message. Btw is mod_rewrite enabled?


* - Mr. Man - Jun 29, 2016

I think its enabled, because when I used the first code I wrote, it was redirecting.

Edit: It's working but images are broken. Should I use absolute URL instead of relative now ?


* - Squito - Jun 29, 2016

(Jun 29, 2016 04:57 AM)Scareface124 Wrote:  Edit: It's working but images are broken. Should I use absolute URL instead of relative now ?

Use URL relative to the root, because if you use something like images/image.jpg, then it will try to find it in domain.com/book/dune/images/image.jpg.


* - Mr. Man - Jun 29, 2016

(Jun 29, 2016 07:02 AM)Squito Wrote:  Use URL relative to the root, because if you use something like images/image.jpg, then it will try to find it in domain.com/book/dune/images/image.jpg.

Oh okay, how do i make the an url optional ? For example, page/1 brings page 1 and page/2 brings page 2 and page must bring page 1 by default. Any idea on this ?


* - Nerdi - Jun 29, 2016

(Jun 29, 2016 08:14 AM)Scareface124 Wrote:  Oh okay, how do i make the an url optional ? For example, page/1 brings page 1 and page/2 brings page 2 and page must bring page 1 by default. Any idea on this ?

Well a band aid fix is to create an index.html in /page/ that redirects to page/1 if someone tries to visit page/. The better fix is through .htaccess by changing the default page for the /page/ directory.


* - Mr. Man - Jun 29, 2016

I added another rule to redirect to page 1 by default and it works. Thanks