I have created a php web site. Here I have implemented the clean URL in .htaccess file. I need to display mu link
mydomain/index.php to mydomain/index
So I have used
RewriteRule index index.php
But I have lot of files. So how will I write a common rule for this entire page?
E:g
RewriteRule hotels hotels.php
Thanks
try this:
#Setup
Options +FollowSymLinks
RewriteEngine On
#Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
#Your thing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
Related
I need to rewrite the url of my web and make it hide subfolder and the .php extension, like this:
original : http://192.168.100.5/project/controller/report.php?unit=1&year=2021
goal : http://192.168.100.5/project/report?unit=1&year=2021
My current rules which doesnt work
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ controller/$1.php [L]
How to achieve this? thankyou
You will need 2 .htaccess files.
site root .htaccess:
RewriteEngine On
# forward everything to controller/ if not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* controller/$0 [L]
controller/.htaccess:
RewriteEngine On
# add .php extension internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
I am trying to remove .php extension for only pages under a subcategory folder of the website.
currently: example.com/blog/my-first-blog.php
what i want: example.com/blog/my-first-blog/
I have tried the following rule in .htaccess (placed in root) but it still shows .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/blog/$1.php -f
RewriteRule ^(.+?)/?$ /blog/$1.php [L]
The best way to achieve result you want is building a routing.
Firstable, you need to rewrite all traffic to your index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
Then, you need to write router that will be parsing URL paths.
The simplest example of router is:
$url = urldecode(preg_replace('/\\?(.*)$/', '', $_SERVER['REQUEST_URI']));
if ($url == '/contact/about') {
include 'contact.php';
}
You can check how does it work in PHP frameworks to get a better solution, or use one of them.
I figured it out
I just had to create .htaccess file under the "/guide/" directory. with the following rule note RewriteBase /guide/ for making sure it only removes .php under "/blog/" folder
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try this --
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.example.in/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.php $ http://www.example.in/$1 [R=301,L]
Try this
DirectorySlash Off
Options -MultiViews -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
i've a php website and using mod_rewrite for clean urls.
my files:
villa.php about.php contact.php index.php 404.php
my villa.php is dynamic and works with mysql db. url example:
domain.com/villa?i=2&s=villaname
i want to use it like domain.com/villa/id/villaname
my .htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
but its not working. what is the right configuration for .htaccess file? any help would be great. thanks for your time.
You can use this rule in your htaccess file
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]
I am working on an .htaccess-file lying in mysite.com/dir/.htaccess that redirects mysite.com/dir/page to mysite.com/dir/page.php if possible and redirect to mysite.com/dir/ if not possible. So any "wrong" request will be redirected to the main page. The code im using is:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [R]
This all works well. (By the way: i want to determine the base-dir of the file, because i maybe want to change the directory name later, but not change the file). What i now want is to block some directories for the user, because they contain php-libraries that should not be accessible. how can i e.g. block the directories mysite.com/dir/lib/* and mysite.com/dir/lib2/* and redirect them to mysite.com/dir/ again like i did before? I also want to use code not like
RewriteRule ^lib/(.)* %{ENV:DIR_BASE}
that would redirect mysite.com/dir/lib/../page to the main page instead of mysite.com/dir/page where it should belong. i tried very much with %{REQUEST_FILENAME} and %{REQUEST_URI}, but i am only a beginner when it comes to mod_rewrite. do you know a solution?
This should work:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
RewriteRule ^lib2?(/|$) %{ENV:DIR_BASE} [L,NC,R]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]
i used the htacess to remove the php extension and it worked but i also have a page called profile wich i shorten but that make the pages names being taken as a a profile user name, is there a way to fix this?
the results i want is like this:
localhost/path_folder/index.php to localhost/path_folder/index
and
localhost/path_folder/profile?username=name to localhost/path_folder/name
and my htaccess code for the removing extension is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
and the code for the profile page is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path_folder/profile.php?username=$1
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
## First try To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /path_folder/$1.php [L]
## if PHP file not found then load profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /path_folder/profile.php?username=$1 [L,QSA]
If you want to remove file extension
RewriteEngine On
RewriteRule ^path_folder/([^/]*)$ path_folder/$1.php [L]
Shorten URL for userprofiles
RewriteEngine On
RewriteRule ^path_folder/profile/([^/]*)$ path_folder/profile?username=$1 [L]