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]
Related
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]
Here is my files
index.php
blog.php
about.php
I am accessing it as
localhost/index.php or localhost
localhost/blog.php
localhost/about.php
How can i rewrite .htaccess so that i can access it by
localhost/
localhost/blog
localhost/about
Update
My image are not displaying once i applied the below provided .htaccess, can any give fix this ?
Simple try this in .htaccess file :
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html
Options -MultiViews # optional Depends on Server
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This is Code,which is use for removing file extension. I want to remove Directory name as well.what to add in this code.
Structure of project= airgle is a index page contains navigation pages.
now,my url is
localhost/demo/airgle/navigation1
i wants localhost/demo/navigation1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Hope this code help you,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost/airgle/navigation1/file_name$
RewriteRule ^$ http://localhost/navigation1/file_name [L,R=301]
You can use this rule in /demo/.htaccess:
RewriteEngine On
RewriteBase /demo/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?! airgle/).+?)/?$ airgle/ [L,NC]
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]
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]