I have learned about URL Rewriting using .htaccess, very recently. As a beginner it has become difficult task for me :(
I can rewrite a specific url, like:
RewriteEngine On
RewriteRule ^nothing/?$ abc.php [NC,L]
But I'm not satisfied with it and need your help.
My .php files are kept in a folder named files. So I have URLs like:
www.mysite.com/files/login.php?lf=student
www.mysite.com/files/login.php?lf=admin
www.mysite.com/files/s_home.php
www.mysite.com/files/recharge.php
What I want is simple:
mysite.com/login
mysite.com/login
mysite.com/s_home
mysite.com/recharge
Can anybody help?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Even though this will do as you've asked, why are you trying to hide the .php extension?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/files/$1.php -f
RewriteRule ^([^/]+)/(.+)/?$ /files/$1.php?lf=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/files/$1.php -f
RewriteRule ^([^/]+)/?$ /files/$1.php [L]
Related
Say I have these pages stored in the root:
page1.php
page2.php
how do I rewrite this:
domain.com/page1/
to:
domain.com/page1.php
P.S.Also need to make sure the rewritten url does not clash with a real directory.
UPDATE:
Solutions below were only working on my remote server, but not localhost Xampp server.
I added this first line to my htaccess and solutions below worked on my local server!
Options +FollowSymLinks +MultiViews
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can use the following Rule in /root/.htaccess :
RewriteEngine On
#skip real files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite /file/ to /file.php if /file/ exists as .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [L]
If you just want to redirect a small number of pages, you could do this:
RewriteRule ^page1/?$ /page1.php [NC,L]
RewriteRule ^page2/?$ /page2.php [NC,L]
You might want to consider something like the following, though, where you pass the number as an argument (assuming you are following a naming convention as you described).
^page/([0-9]+)/?$ /pageLoader.php?p=$1 [NC,L]
I've just started to use .htaccess and i'm having an issue here.
Inside my public_html folder i have these files.
index.php
profile.php
test.php
.htaccess
And when i go to profile.php file i have some parameters.
http://website.com/profile.php?id=1&name=Doe
For showing better SEO links i'm trying to make it appear like this
http://website.com/1/Doe
with this rewrite condition
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>
depended on this answer (click here)
and then in my php file i get the id of the user so i can make the queries etc with this code
$path_components = explode('/', $_GET['url']);
$ctrl=$path_components[0];
But the thing is that if i do so every file in my folder is trying to make the Rewrite Rule but i want a specific file... the profile.php one.
EDiT 1:
So from the comment below i made this htaccess file, and regardless the mod_rewrite is enabled on my server, the link is not changing at all.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/profile.php$ [NC]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /profile.php?id=$1&name=$2 [L]
</IfModule>
And my php file now..
$path_components = explode('/', $_GET['id']);
$ctrl=$path_components[0];
But the link
http://website.com/profile.php?id=1&name=Doe
is not changing at all.
You need to redirect your old url to the new one, put this above your existing rule
RewriteEngine on
RewriteCond %{THE_REQUEST} /profile\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /profile.php?id=$1&name=$2 [L]
Hello the issue is simple: you should exclude existing files/folders from rewriting process.
I would recommend to have only one index.php page where you get all the request and process in one place. Anyway your working htaccess file should look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# exclude real folders/files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^ index.php [L] - # redirect anything to index
# for your version
RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>
I'm trying to rewrite a url such as:
www.domain.com/file/name.php?id=1
to without the .php like this:
www.domain.com/file/name?id=1
I have tried altering the htaccess that's placed in the file directory to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
But it just doesn't work and every time I've tried typing in www.domain.com/file/name?id=1 I get a 404 error.
I've tried all the answers on here relating to this to make sure its not a duplicated question and also double checked to make sure mod rewrite is enabled but I'm at a complete loss now!
Any help would be much appreciated.
Try this: RewriteRule ^name$ name.php
This is from my own .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^name$ name.php
</IfModule>
I'm trying to change some of the url names using htaccess.
ex;
mg_com_tr/index.php to mg_com_tr/home
I tried many different code samples inside my htaccess file, but nothing seems to work.
I never worked with the htaccess file before,so in order to test it, I found some example codes just for removing the ".php" extension.
When I try this code, I'm not getting any errors, but its not removing the .php extension either.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The only working code I found is this one;
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
This removes the .php extension, however whatever I try I couldn't manage to change
http://localhost/mg_com_tr/index.php
into
http://localhost/mg_com_tr/home
I have a feeling that it has something to do with the path
Both the index.php and htaccess file is located at
D:\wamp\www\mg_com_tr\
try
RewriteEngine on
RewriteRule ^home index.php [NC,L]
or
RewriteRule ^home /index.php [NC,L]
I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.
Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.
But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.
I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Thanks in advance :)
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC]
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]
#other existing rules go here