rewrite htaccess for clean url and custom 404 page - .htaccess

I'm trying to do four things with .htaccess
First thing is removing the php extension from all files
so
www.mywebsite.com/home is written www.mywebsite.com/home.php
I want to change to error page so if a request is made to www.mywebsite.com/adakadabra and there is such page like adakabra.php a custom 404.php is used unstead.
Thirdly I would like to have vain url such that users can have profile links like
www.mywebsite.com/Joe
I have a profile.php and I use a get request to get the username so the rewrite would be from
www.mywebsite.com/joe to www.mywebsite.com/profile.php?username=joe
Last but not least
I would want to have a pages like www.mywebsite.com/p/123453
to rewrite to
www.mywebsite.com/p.php?photo=123453
I have the page p.php on my webserver.

This can to it, with 3 sets of rules in /.htaccess:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([0-9]+)$ /p.php?photo=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Joe|Mao|Napoleon|Gandi)$ /profile.php?username=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /$1.php [QSA,L]
Here how it works:
www.mywebsite.com/p/123453 => /p.php?photo=123453
www.mywebsite.com/Joe => /profile.php?username=Joe
www.mywebsite.com/home => /home.php,
www.mywebsite.com/houra/foo/bar/home => /houra/foo/bar/home.php

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]

htaccess rewrite rule to hide actual file

This is my folder structure
htdocs
-> testing
->index.php
->.htaccess
Inside index.php I have this code
<?php
if(isset($_SERVER['PATH_INFO'])) {
echo $_SERVER['PATH_INFO'];
}
?>
Inside .htaccess I have this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
Here is my question:
In the above htaccess rewrite rule, which will link my address localhost/testing/HelloWorld to localhost/testing/index.php/HelloWorld
Is it possible to do some thing like this? When the user enter localhost/testing/index.php/HelloWorld I want the browser to hide the word index.php and only display something like this localhost/testing/HelloWorld
I tried to redirect the link localhost/testing/index.php/HelloWorld to localhost/testing/HelloWorld by adding another rule in .htacces file but I get "The webpage has redirect loop" error message.
After I add in new rule the .htacces file looks like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/(.+)$ /testing/$1 [R]
Does any ways that I could make my link like this? Thanks in advanced, your help will be very much appreciated.
You get the redirect loop, because the rewritten url matches the rule for redirection. You'll need to use a trick to make the external redirect only happen on an external request with index.php, not on an internal rewrite that maps a different request to that file. You can do this with %{THE_REQUEST} which will only ever be whatever the external request was. It will not update if you rewrite the file it maps to.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /testing/index\.php/
RewriteRule ^index.php/(.+)$ /testing/$1 [R]

Redirect all requests to index.php and clean the URL

I was wondering if it's possible to redirect all requests to files/directories that do not exist to index.php, and after that clean the url.
So when I go to www.example.com/test/1 I want it to be redirected (rewrote?) to www.example.com
I tried the following and it does correctly load index.php, but it still says www.example.com/test/1 in the URL bar...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Of course I still want my css and js to load properly...
Try this. If I understand, you want to use this as a way of redirecting a 404 not found?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [R=301,L]
Or maybe this without index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]

friendly urls with .htaccess

I am using the following .htaccess code in my website to redirect all the urls to index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
then I am checking the url and see if the name of the page is available in the database. Then I am redirecting the user the that specific page if it is found like so:
www.mywebsite.com/videos.php?v=Name_of_the_video
www.mywebsite.com/images.php?i=Name_of_the_image
www.mywebsite.com/users.php?u=Name_of_the_user
as you can see I have 3 main types of pages. I want to use .htacces so I can convert those urls like so:
www.mywebsite.com/videos/Name_of_the_video
www.mywebsite.com/images/Name_of_the_image
www.mywebsite.com/users/Name_of_the_user
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])([^/]+)/(.*) /$1$2.php?$1=$3 [L,QSA]
You'll probably want to put this before your other rules though.

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