htaccess rewrite going to 404?

Viewed 59

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]

I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4

It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php

Just cant seem to figure out what to do. Yes I do have mod-rewrite on.

1 Answers

With your shown samples, please try following htaccess rules file.

This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.

Make sure to clear your browser cache before testing your URLs.

RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^  %1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]


2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.

RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
Related