5

I would like to use localhost/projects/project1/page/x instead of localhost/projects/project1/index.php?page=x. But after I had redirected the URL in .htaccess using RewriteRule, it returned my 404 error page.

Here is my code:

ErrorDocument 404 /projects/project1/pagenotfound.php

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/project1/page/([0-9]*)$ /projects/project1/index.php?page=$1 [L,QSA,NC]

SOLUTION

ErrorDocument 404 /projects/project1/pagenotfound.php

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([0-9]*)$ /projects/project1/index.php?page=$1 [L,QSA,NC]
8
  • Temporarily add an R=302 flag on the RewriteRule (it will now externally redirect, rather than internally rewrite, and make it easier to debug) - do you see the redirect? Do you still get the 404?
    – MrWhite
    Commented May 14, 2015 at 0:49
  • What happens when you get rid of QSA in your flags? After all, the friendly url has no query string attached to it. Commented May 14, 2015 at 6:49
  • Still not working. I tried added R=302 flag and still get the 404. Also tried it without QSA, still get 404. Commented May 14, 2015 at 21:53
  • And when you add the R=302 what URL are you redirected to? ...What URL triggers the 404? /projects/project1/index.php?page=0?
    – MrWhite
    Commented May 14, 2015 at 23:41
  • 1
    If you are seeing localhost/projects/project1/page/1 in the browsers address bar, after adding the R=302 flag then the redirect/rewrite is not happening. Is mod_rewrite enabled? Is AllowOverride set appropriately in your server config? Where is your .htaccess file located?
    – MrWhite
    Commented May 15, 2015 at 14:15

1 Answer 1

2

My .htaccess is in the same directory as the index.php, so: localhost/projects/project1/.htaccess

That's the problem, or rather your RewriteRule pattern is the problem, depending on how you look at it.

In .htaccess (per-directory) files, the per-directory prefix is first removed from the URL before matching against the pattern. So, if your .htaccess file is at /projects/project1/.htaccess, then your RewriteRule pattern needs to exclude /projects/project1/ from the start (for the same reason you remove the / prefix from the pattern when in the document root). In other words...

RewriteRule ^page/([0-9]*)$ /projects/project1/index.php?page=$1 [L,QSA,NC]

Or, move your .htaccess file to the document root.

3
  • 1
    @MártonJuhász If an answer "works" it is both polite and helpful to vote the answer up, as well as accepting it.
    – Fred Gandt
    Commented Jun 4, 2017 at 18:36
  • @FredGandt Thanks, much appreciated. However, a user requires 15+ rep in order to "vote up", even on answers to their own questions unfortunately.
    – MrWhite
    Commented Jun 4, 2017 at 21:00
  • 1
    My derp; completely forgot. I'll vote the question up too, and hope they get back to you ;-)
    – Fred Gandt
    Commented Jun 4, 2017 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.