Search code examples
php.htaccessurlurl-rewriting

Url rewrite rules error: The requested URL was not found on this server


I am create the rules in the .htaccess file in my domain folder.

My url on server is :

https://www.everyoneknows.com.my/customer/?loc=dashboard

And I want change url to:

https://www.everyoneknows.com.my/customer/dashboard/

In the .htaccess file, I write the rules are:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^everyoneknows\.com\.my [OR,NC] 
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.everyoneknows.com.my/$1 [R,L]

##External redirect to /customer/dashboard url rules here.
RewriteCond %{QUERY_STRING} ^loc\=dashboard$
RewriteRule ^customer/$ https://www.everyoneknows.com.my/customer/dashboard/? [R=301,L]

And the error message is shown:

Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

picture

Hope someone can guide me on how to correct in my rewrite url rules. Thanks.

Original result:

enter image description here

Updated 1

enter image description here


Solution

  • You need a redirect rule for old URL to new and an internal rewrite rule to handle pretty URL:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^everyoneknows\.com\.my [OR,NC] 
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^ https://www.everyoneknows.com.my%{REQUEST_URI} [R=301,NE,L]
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} \s/+(customer)/\?loc=([^\s&]+) [NC]
    RewriteRule ^ /%1/%2/? [R=301,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(customer)/([\w-]+)/?$ $1/?loc=$2 [L,QSA,NC]
    

    This assumes there is no .htaccess in customer/ subdirectory. Make sure to clear your browser cache before testing this change.