How can hide directory name from the url using htaccess? - php

For example I have url like
http://localhost/Experiments/login/login.php
I have hide the extention of file putting below code in .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.css -f
RewriteRule ^(.*)$ $1.css
RewriteCond %{REQUEST_FILENAME}\.js -f
RewriteRule ^(.*)$ $1.js
RewriteCond %{REQUEST_FILENAME}\.jpg -f
RewriteRule ^(.*)$ $1.jpg
RewriteCond %{REQUEST_FILENAME}\.ico -f
RewriteRule ^(.*)$ $1.ico
how can I make the above url to http://localhost/login
Even I tried for the solutions related to the same issue in stackoverflow but could not make it

Insert this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/Experiments/login/$1.php -f
RewriteRule ^([\w-]+)/?$ Experiments/login/$1.php [L]
# remaining rules go below this

Related

Remove extensions for both .html and .php in apache

The code is mixed to I have .html and .php in the same folder. There is never a time there there is the same name but different extensions. I have some code that should work but doesn't. The html part works but when I try to request a PHP page without an extension the URL it gives a 404
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -F
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -F
RewriteRule ^(.*)$ $1.php
You can try this on the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]

Rewrite rule that will get rid of the .php file extension in my URL

I need a rewrite rule that will get rid of the .php file extension in my URL. I've tried the following. It doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
According to this the following code should work for Removing PHP Extensions.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To remove the .php extension completely , you can use the following rules :
RewriteEngine on
#1 redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^ %1 [L,R]
#2 internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

Ignoring subfolder in URI

I have a problem with .htaccess configuration.
I know how to ignore file extensions (e.g. php).
That's my code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
But I want to ignore specific a subfolder in the URI.
For example, I have a file in example.com/sites/test.php but in the address bar, I just want to type example.com/test. Any ideas?
You can change the RewriteRule
RewriteRule ^(.*)$ sites/$1.php
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sites/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /sites/$1.php [L]

htaccess rewrite give error 500 in localhost

I've been searching on the web but nothing seems to work. I'm working in local with Apache and Xampp. I have created an .htaccess file and rewrote all urls ending with .php and .html to remove the extension and I have created a custom 404 page. Everything worked fine. However, now I'm trying to rewrite a dynamic url that looks like
/TotinCoblan/update/Wine?id=2
to
/TotinCoblan/update/Wine/2
and I keep getting error 500. this is my .htaccess file. From what I understood,
RewriteCond %{REQUEST_FILENAME} !-f
should avoid infinite loops.
Any help would be much appreciated.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
ErrorDocument 404 TotinCoblan/errors/404
Try this code:
ErrorDocument 404 /TotinCoblan/errors/404
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I finally used this code, I had to use RewriteCond %{REQUEST_FILENAME} !-f since the page was loading only after reloading two time. With that instructions loads the first time.
RewriteCond %{REQUEST_FILENAME} !-f
ErrorDocument 404 /TotinCoblan/errors/404
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [L]
RewriteRule ^update/Wine/([0-9]+)/?$ update/Wine.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
You should change your -f checks to use %{DOCUMENT_ROOT}${REQUEST_URI} instead. The problem with %{REQUEST_FILENAME} is that mod_rewrite will partially attempt to map the URI to a file, and it accounts for PATH INFO, which means if the request is something like:
/something/foo/anything/
and you happen to have an existing file/script at:
/something/foo.php
then the RewriteCond %{REQUEST_FILENAME}\.php -f check will be true, and the URI that it assumes you meant is:
/something/foo.php/anything/
And, like your code is doing, the .php is appended to the end instead:
/something/foo/anything/.php
and that causes the infinite loop. Try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
ErrorDocument 404 TotinCoblan/errors/404

htaccess returns Internal Server Error

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
The rewrite rule for the api/list/episode comes up with an Internal Server Error I'm not sure why but the other rule works but I have tried to change
this
http://domain/api/list/episode?season=1
to this
http://domain/api/list/episode/1
Is it is right for me to place the .htaccess in my root for all directory changes?
That is because of looping. Change that rule slightly:
RewriteEngine on
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^api/list/episode/([^/]+)/?$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Try changing the order of your rules. The two /api/ rules should go before the routing-to-php-extension rule. This is because %{REQUEST_FILENAME} actually inspects inner path elements so it could mistaken /api/list/episode/1 as /api/list/episode.php/1 as "exists", then you tack on a .php to the end, which makes it: /api/list/episode/1.php. It goes through the rewrite engine and the same thing happens again, and then the loop goes on and on.
RewriteEngine on
RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can also try changing you -f check to:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f

Categories