How to hide folder names and file extension in URL? - php

I have following requirements which i believe can be accomplished using .htaccess file.
Requirements-
Hide Folder names and file extension in URL Eg. www.example.com/subfolder/subfolder1/file.php should become www.example.com/file
Restrict Folder browsing - I want to restrict folder browsing capability when somebody fires an URL eg. www.example.com/subfolder Conventionally by firing this URL user will be able to browse through the contents of subfolder. By firing such URL or any URL containing domain example.com eg. www.example.com/folderNotExist then server should redirect to index page.
I am able to restrict folder browsing but redirection to index page and hiding of folder and file extension is not working.

You can have these rules in your root .htaccess:
ErrorDocument 404 /
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} /subfolder/subfolder1/ [NC]
RewriteRule ^ / [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/subfolder/subfolder1/$1.php -f
RewriteRule ^(.+?)/?$ subfolder/subfolder1/$1.php [L]

You should try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /subfolder/subfolder1/$1.php
ErrorDocument 404 /index.php

you can try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mysample.com
RewriteRule ^subdir/(.*)$ http://www.mysample.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f

Related

Htaccess code redirects to XAMPP index page

I am trying to write htaccess codes which redirect all URLs apart from directly linked files (e.g images & css files) to my index.php file for processing
http://localhost/testsite/login
redirects to
http://localhost/testsite/index.php?cmd=login
But the htaccess code instead redirects me to the XAMPP homepage. This is my htaccess code
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?cmd=$1 [QSA,NC,L]
What am I doing wrong?
Remove / from target and use proper RewriteBase:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /testsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?cmd=$1 [QSA,L]
/index.php will route it to site root index.php instead of index.php in current directory.

301 redirect URLs that are also being rewritten

I am building a site using Slim, which suggests the following .htaccess code for creating pretty URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
This means that a URL like http://example.com/account/login will alias for http://example.com/index.php/account/login, which is then processed by my front controller. So far so good.
However, if someone explicitly navigates to
http://example.com/index.php/account/login,
I would like it to first redirect to:
http://example.com/account/login,
and then alias for
http://example.com/index.php/account/login.
That way, the user will see http://example.com/account/login in their navigation bar.
How can I handle both of these rules simultaneously in .htaccess, and most importantly, without hardcoding the host and domain (http://example.com) in .htaccess?
This should also work in a subdirectory, for example:
http://example.com/site1/index.php/account/login ->
http://example.com/site1/account/login
With the .htaccess file located in the site1 directory relative to document root, and without having to explicitly put site1 in the .htaccess.
You need a new redirect rule before this rule:
RewriteEngine On
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Try this:
RewriteRule ^index\.php/(.*)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Redirect to page but keep original URL using htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^test\.localhost/$ [NC]
RewriteRule ^(.*)$ http://test\.localhost/err.php/$1 [NC,L]
This works. When I type test.localhost/qwieoqjdoiqwje it redirects me to:
test.localhost/err.php/qwieoqjdoiqwje
But I want to redirect me to redirect me to test.localhost/qwieoqjdoiqwje and also load err.php . Is this possible via htaccess? Thanks1
You can accomplish this by redirecting all requests to one file with .htaccess
<IfModule mod_rewrite.c>
# Block access to hidden files
RewriteRule "(^|/)\." - [F]
# Redirect requests that are not files or directories to index.php.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</IfModule>
Then in your index.php use the path to execute whatever function you want.
if ($path == 'error' || $path == 'hello/world')
{
myerrorfunction();
}
This is of course a very simple solution. You would probably want to create an array that maps a path to a funciton. this is how most cms systems acomplish this task.

.htaccess mod_rewrite in main directory and subdirectory

I've a main directory with a .htaccess file that looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The one in the subdirectory looks identical to this one. What I want the .htaccess files to do is redirect to the index file in each directory.
So if you visit main/example you get redirected to the index.php file in the main directory but if you visit main/subdirectory you get redirected to the index.php file in the subdirectory.
I've searched for an answer but haven't found anything that works.
Thankful for any help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]
This configuration do the following:
if we have this files
/index.php
/foo/index.php
/var/index.php
/var/boo/index.php
and you request the follow file
/foo/hello
you will go to:
/foo/index.php
and, if you request
/var/boo/world
you will go to:
/var/boo/index.php
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
You can use a simple solution like this one, no need to put .htaccess files everywhere
on your main directory you put one .htaccess file, you keep your code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
and you add those two lines
Options All -Indexes
ErrorDocument 403 http://domain.com/index.php
these two lines prevent the user to access the folder, and returns the statut 403 and then you redirect your 403 statut to the adress of your index.php of your main directory

htaccess redirecting all url to default profile page

i am having problem in my .htaccess. what happened was that changed htaccess by adding this :-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
to it but after that it also redirects all url after the domain to some default like profile page for eg : if i enter domain.com/edit.php it shows profile.php page but edit.php does not exist rather it should show 404 page but that does not happen.
Here is my .htaccess , is it right and is it affecting my SEO also.
Options +Indexes
# or #
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9._-]+)$ profile.php?u=$1
RewriteRule ^([a-zA-Z0-9._-]+)/$ profile.php?u=$1
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
means that if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist then proceed to the rewrite rule immediately below these strings
In your case the rule is
RewriteRule ^([a-zA-Z0-9._-]+)$ profile.php?u=$1
So if you want anything specific happen in case of not found page, add some other rule (redirect to 404 page, etc) just under the string with -d and -f flags.
Or remove these strings and add a link to your 404 error document like this
ErrorDocument 404 /errordoc.php
or link index.php as error document
ErrorDocument 404 /index.php

Categories