skip to Main Content

I am trying to implement clean URL with the help of .htaccess in my project.

I got a 404 when i implimented a condition with multiple condition strings like keyword1/keyword2/param

all other conditions like RewriteRule ^home index.php [L,NC] works fine

My file structure be like

/subdirectory/
            |-.htaccess
            |-index.php
            |-edit-user.php
            |-new-user.php

my desired clean url is

mysite.com/subdirectory/user/edit/10

and it should translated into

mysite.com/subdirectory/edit-user.php?id=10

Some of the closest solutions i tried so far (but no luck)

RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]


RewriteBase /user/
RewriteRule ^user/edit/([0-9]+)$ edit-user.php?id=$1 [L,NC]

Any suggestions are highly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Finally found the issue.

    My .htaccess was

    RewriteRule ^home  index.php [L,NC] RewriteRule ^([^.]+)$ $1.php [NC] 
    RewriteRule ^user/edit/(d+)$ edit-user?id=$1 [L] 
    

    (1st line to add .php to anything that comes in, and the 2nd line to convert the desired URL I needed)

    What happened here is, when I try to access the URL mysite.com/subdirectory/user/edit/10

    The first rule converts that into mysite.com/subdirectory/user/edit/10.php instead of mysite.com/subdirectory/edit-user.php?id=10

    This causes the 404 error.

    Now I changed the order and the new .htaccess file looks like,

    RewriteRule ^admin/edit/(d+)$ edit-admin.php?aid=$1 [L]
    RewriteRule ^([^.]+)$ $1.php [NC]
    

    So, when a URL comes in, it will check into all other rules before its matches against the last rule(which appends .php) and translate into the desired result.

    Lesson learned: Order matters a lot in .htaccess


  2. RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]
    

    Since the .htaccess file is inside the /subdirectory then you would need to write the directive like this:

    RewriteRule ^user/edit/(d+)$ edit-user.php?id=$1 [L]
    

    And remove any RewriteBase directive.

    d is simply a shorthand character class for [0-9].

    The RewriteRule pattern matches against the relative URL-path (no slash prefix). That is relative to the directory that contains the .htaccess file. You were also missing the .php extension on the filename you are rewriting to. You do not need the NC flag unless you really do want to allowed a mixed-case request, but that opens you up to potential "duplicate content" which would need to be resolved in other ways.

    RewriteBase /user/
    RewriteRule ^user/edit/([0-9]+)$ edit-user.php?id=$1 [L,NC]
    

    Actually, you are very close here, but the RewriteBase directive would have caused this to fail. The sole purpose of the RewriteBase directive is to override the directory-prefix that is added back on relative path substitutions. The RewriteBase directive sets the "URL-path" (as opposed to filesystem path) that is added back.

    So, in this example, RewriteBase /user/ would result in the request being rewritten to /user/edit-user.php?id=10 (relative to the root), which is clearly wrong based on the file structure you posted.

    Without the RewriteBase defined then the directory-prefix is added back, which results in the rewrite being relative to the directory containing the .htaccess file.

    Also, there’s no need to backslash-escape slashes since there are no slash delimiters to the regex. (The spaces that surround the argument are the delimiters.)

    all other conditions like RewriteRule ^home index.php [L,NC] works fine

    Careful with this, as this will also match /homeanything and /home/something etc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search