I Currently have 2 rules:
The first replace "domain.com/profile/user" to "domain.com/profile.php?user=user"
The second rule removes the .php from all of the files so that they can be accessed without the need for .php.
Here is the .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^profile/([^/]+)$ profile.php?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
In its current state, the first rule works and the second does not work.
However, if i add "Options +MultiViews" to the top so that it is:
Options +MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^profile/([^/]+)$ profile.php?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Then the first rule does not work while the second one does work?
I just can't figure out how to get them both working.
Note i am using a XAMPP vhost for the web server and have changed the relevant settings to allow htaccess to work with XAMPP.
Ok, I have found a solution, not the best but it works.
I simply changed my .php remover to another version which achieves the same task.
I don't know why this works and the other version doesnt. If you know why the origional one does not work then please let me know.
Here is the working version:
RewriteEngine On
RewriteBase /
RewriteRule ^profile/([^/]+)$ profile.php?user=$1 [L,QSA]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Source: https://www.digitalocean.com/community/questions/how-to-hide-php-extension-in-url-using-htaccess
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 am trying to rewrite the url using htaccess and I have tried answers given on another questions here however nothing seems to be working at all I still get the original url.
this is what I have:
http://localhost/inbox.php?pg=2
I want
http://localhost/inbox/2
I already had a rule that gets rid of the .php extension in .htaccess as below and just added the last line
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this
Your problem is that the line before the last one is defined as the last one so your rule must be above that RewriteConditions. Better use this rule set:
RewriteRule ^/?inbox/(\d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
I added your needed prefix which you missed and made it mandetory that after that numbers will follow (at least one).
Apache rewrite engine is mainly used to turn dynamic url’s such as http://localhost/inbox.php?pg=2 into static and user friendly url’s http://localhost/inbox/2
RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]
Explanation: How this work
Call to action: RewriteRule
Pattern: ^inbox/([^/.]+)/?$
Rewrite: /inbox.php?pg=$1
Command Flag: [L]
Simply,
RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1
Source: 10 Simple examples to rewrite using htaccess
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've a question, I'm developing a webservice for a specific app.
Last week I transferred my back-up from server1 to server2, since that moment my .htaccess won't work anymore.
The url for my webservice is:
http://apps.holtesdesign.nl/inholland/apiv0/
This url works fine, to load the bugreport module:
http://apps.holtesdesign.nl/inholland/apiv0/bugreport.php
But the url was supposed to be:
http://apps.holtesdesign.nl/inholland/apiv0/bugreport
When you'll open the last url, you'll see that it will result in an internal server error.
Do you know how it's possible?
My .htaccess looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The server is running on CentOS, and I've already checked if mod_rewrite is functioning...
I hope you can help me guys!
Cheers,
Jelmer
This will fix it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
If it doesn't work correct the settings of your Wamp Server:
Left click WAMP icon
Apache
Apache Modules
Left click rewrite_module.
This will hide the trailing slash and hide .php in root & sub directories.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Working Concept for me:
# Options is required by Many HostSevice
Options +MultiViews
RewriteEngine on
# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Use this code in Root folder of your website in .htaccess File : Example
offline - wamp\www\YourWebDir
online - public_html/
Important Note : If it doesn't work correct the settings of your Wamp
Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left
click rewrite_module
try this.. it works! :)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I am quite new to PHP and just getting started with mod_rewrite. I know the basic lingo but come stuck when I want to simply refer to the route directory
i.e. this is not probs
RewriteRule ^settings/$ settings.php
[QSA,L]
But how to for example make:
RewriteRule ^page/(.*)$
index.php?Page=$1 [QSA,L]
which generates /page/[page-name]
Just become
/[page-name]
?
Maybe I didn't understand you but it seems that you need such .htaccess file to solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Ignore valid files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Page=$1 [QSA,L]
</IfModule>
This should do it:
RewriteRule ^(.*/)$ index.php?Page=$1 [QSA,L]
However, you should place that rewrite rule after all the other specific rewrite rules you have, otherwise all requests will be redirected to index.php?Page=....