Error 404 - Not Found when URL change by .htaccess - .htaccess

I have changed the URL using the .htaccess file. When go to that URL it gives Error 404 - Not Found
.htaccess code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Product/Details/([^/]*)/(.*)/?$ index.php?u=$1&p=$2 [QSA,L]
Change aaa.com/Product/Details/index.php?u=aa&p=bb to aaa.com/Product/Details/aa/bb.
How to avoid ERROR 404 and get URL data by $_GET['u'] and $_GET['p'] or using other method.

This probably is what you are looking for:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^u=([^&]+)&p=([^&]+)$
RewriteRule ^/?Product/Details/index\.php/?$ /Product/Details/%1/%2 [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?Product/Details/([^/]+)/([^/]+)/?$ /index.php?u=$1&p=$2 [QSA,L]
It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues.

Related

htaccess rewrite going to 404?

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.
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]

Trying to make an internal redirect from "example.com/about" to "example.com?p=about"

I am trying to make an internal redirect (without changing the url in the client-browser) from "example.com/about" to "example.com?p=about".
I added the the folowing code to my .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\?
RewriteRule ([^\/]*)$ http://%{HTTP_HOST}?p=$1 [L]
But if I open the page (example.com/about), I am getting an internal server error
Does anyone know what I am doing wrong?
Thanks for helping
Edit: If I add the R flag to the RewriteRule, everything works fine
You got error because your rule just add query string then capture again any URI even passed through this rule before so,Your rules should look like this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ http://%{HTTP_HOST}?p=$1 [L]
And to be more specific and able to handle real wrong requests by any other rule , do this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !p=(.*)
RewriteRule ^([^/]*)$ http://%{HTTP_HOST}?p=$1 [L]

.htaccess rewrite for url parameters?

i have been trying to make my urls "pretty" / human readable, the urls at the moment are:
[BASE_URL]/?action=viewProposal&proposaltitle=tesst
I want to rewrite them to be just [BASE_URL]/tesst
I tried using the following code and modifying it but it wouldn't work, ie. it didn't redirect the pages but didn't throw any errors.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Will the PHP GET functions still work properly as ?action defined whether its a view / edit / delete?
I am assuming base url your index.php try following rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?action=viewProposal&proposaltitle=$1 [QSA,L]

Redirect rule case insensitive

I have URL rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]
When this URL is called: http://example.com/suyash
It internally calls http://example.com/sites/suyash but keeps the URL as http://example.com/suyash only. Please note that the folder sites/suyash actually exists
The issue I am facing is that when I call a URL: http://example.com/Suyash (Uppercase) it shows a 404 error.
When I use the mod_speling module and add the following to the code:
CheckSpelling On
and now when I call http://example.com/Suyash it changes the URL to http://example.com/sites/suyash
How do I get it to only change URL from http://example.com/Suyash to http://example.com/suyash?
mod_speling will redirect the browser in order to fix spelling, there's no way to avoid this. The only thing I can think of is to try proxying the request internally by using the P flag. You'll need to make sure you have mod_proxy loaded. I haven't tested this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L,P]
I would say keep CheckSpelling off.
And then have your rule like this:
CheckSpelling Off
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$0 -f [NC]
RewriteCond %{DOCUMENT_ROOT}/$0 -d [NC]
RewriteCond %{REQUEST_URI} !^/sites/ [NC]
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [L]

htaccess physcially redirect in case of 404

To start off I know how to do a simple redirect to a 404 when a page doesn't exist with.
ErrorDocument 404 /index.php
Is what I want to do is a physical redirect to the index page. So if a user types in mydomain.com/abc and that page doesn't exist it does an actual redirect TO the index page. I don't want the url to remain the same with the bad address. Yes I know its not a big deal but it bugs me. I could accomplish this by having a custom 404 page redirect to the homepage but I don't want this.
I've tried this with the [R] redirect flag and obviously it doesn't work.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.*)$ /index.php [L,R]
Update
This is the error I get it rewrites the url to mydomain.com/index.php
I tried to remove the index.php with just / and it didn't redirect but gave the same error below.
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]
Explanation:
checks whether the file has extension:
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
If not, checks whether file is present:
RewriteCond %{REQUEST_FILENAME} !-f
If not a file, checks whether it is a folder which is present:
RewriteCond %{REQUEST_FILENAME} !-d
If not append .php. If / is present at the end, remove it and append .php
RewriteRule ([^/]+)/?$ $1.php
Checks whether the .php appended or whatever extension file is actually a file which is present:
RewriteCond %{REQUEST_FILENAME} !-f
check whether it is a directory:
RewriteCond %{REQUEST_FILENAME} !-d
If not, then it is a invalid file request and redirects it to /
RewriteRule ^(.*)$ / [L,R]
This should be your mod_rewrite based 404 handling:
# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]
However I am not sure what are you doing in your first rule? Are you trying to add .php to each request URI?

Resources