Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Help with URL Rewriting

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 ?
You got the order wrong in RewriteRule.
PHP Code

<?php 
RewriteEngine On
RewriteRule 
^/book/(.*)$ /book.php?name=$1 [NC] 
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
(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.
Is book.php in the root? Isn't there a typo in the filename of the PHP file?
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
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.
Check the apache error.log, it's usually more descriptive, than a usual 404 error message. Btw is mod_rewrite enabled?
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
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 ?
(Jun 29, 2016 04:57 AM)Mr. Man 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.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
(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 ?
(Jun 29, 2016 08:14 AM)Mr. Man 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.
I added another rule to redirect to page 1 by default and it works. Thanks


Possibly Related Threads...
Thread / Author Replies Views Last Post
by a_different_usernameJan 7, 2019 12:27 PM 0 164 Jan 7, 2019 12:27 PM
Last Post: a_different_username
by SkidyAug 26, 2016 02:45 AM 4 199 Aug 26, 2016 02:45 AM
Last Post: DJW09
by UnderworldproblemJan 10, 2016 05:27 PM 10 221 Jan 10, 2016 05:27 PM
Last Post: Rushax