What I am trying to do it simple:
I want to remove .php extension from file for which I am using following code and it is working
#for hiding.php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
############################-------- END ---------########################
Also I want to show a $_GET parameter on screen for which the link should be link this
localhost/123 which is representation of localhost/somepage.php?para=123
and for which I am using following code which is also working fine
#for parameter Display
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ somepage.php?para=$1
############################-------- END ---------########################
Problem: These codes are working fine when not written together in same `.htaccess` file. But when written together and When I have to access login page using localhost/login. .htaccess treat login as get parameter value. Any solution to this problem?
Try this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ somepage.php?para=$1 [L,QSA]
Related
I have a lot of php files, but I stuck in this that I want to hide php extension from url in for all files without the need to edit my code.
Another problem is that the website use $_GET to caught many parameters from links, headers, forms, is there a way to hide parameters from url or at least to change how it looks without effecting the website functionality.
I tried this but didnot work :
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php\?pid=([0-9]+)
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php(\ |$)
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)/([0-9]+)$ /$1.php?pid=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try out below code in .htaccess file
# Turn on the rewrite engine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thanks.
Hi I am making a site where I want nice and clean links. I am trying to make a link with a get parameter look alot cleaner. Here is a sample link of how the link is at the moment:
http://www.example.com/index.php?item=cd-player
Here is how I want the link to look:
http://www.example.com/cd-player
I have been able to get rid of index.php from the url to leave just the parameter but I need help in getting rid of the "?item" bit as well.
Here is what I have tried so far:
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^(.+?)/?$ /?item=$1 [L,QSA]
Also, this may be a php code rather than a code for the .htaccess file, but if a user enters http://www.example.com/cd-player, how will it know that "cd-player" is the value of a get variable called "item"?
EDIT: The problem I am having with the answers below is that I am using the following code to remove file extensions and the code in the answers below treats the files as a query:
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this instead :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?item=$1 [L]
You need 2 rules actually:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?item=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?item=$1 [L,QSA]
You need to do it like this. This will allow you to have clean links
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)$ $1.php [NC,L]
#This means if its not a real file
RewriteCond %{REQUEST_FILENAME} !-f
#This means if its not a real directory
RewriteCond %{REQUEST_FILENAME} !-d
#Then take the capture and rewrite it
RewriteRule ^(.+)$ /index.php?item=$1 [L]
I want to convert an URL like this:
http://somesocial.com/SomeSocial/success/profile?search_user=exampleuser01
Into a personalized URL like this: http://somesocial.com/exampleuser01
I've tried creating a folder with a file inside success/preview/user_preview.php
which is a copy of profile.php to get the parameter from 'search_user' with $profile_id = $_GET['profile_id'] and header('Location:'.'success/profile?'.$profile_id); To redirect the searched user for his original link.
Modified my .htaccess to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^success/preview/user_preview?search_user=
RewriteRule ^/?([^/]+)$ success/preview/user_preview?search_user=$1 [L]
This works perfectly, but every time I try to access a file from the main dir (i.e Index.php) it redirects me to http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php
Why?
Try reordering your rule wit more restrictive conditions:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([^/]+)/?$ success/preview/user_preview?search_user=$1 [L,QSA]
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'm working a small project in which i'm using .htaccess to make URLs short and sweet. But it's not working. Here's the code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php #removing .php extension
RewriteCond %{SCRIPT_FILENME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category/(.+)$ category.php?category=$1 #removing get variables
By using the first condition it's removing the use of .php extension in the URL bar. And the second condition is added with an intention to remove the GET variables. URL is something like this:
www.example.com/category.php?category=latest
and i want this url to be look like this:
www.example.com/category/latest
Flip the order otherwise first rule will catch this category URI also.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/(.+)$ category.php?category=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]