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]
Related
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
I would like to find a RewriteRule that does this :
mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS
I used such a .htaccess file :
RewriteEngine On
RewriteRule ^(.*) index.php?id=$1
But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.
What RewriteRule should we use to do such URL shortening ?
Here is what the index.php page (I didn't mention the headers) looks like :
<body>
Bonjour <?php echo $_GET['id']; ?>
</body>
Try the following your .htaccess file, as it is the setup successfully used on my own website.
# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?id=$1 [L]
There are 2 htaccess files:
Keep your htaccess file within application folder as:
Deny from all.
and paste following code to your htaccess file outside the application folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It is working perfect for me.
You need RewriteCond to stop rewriting for real files and directories:
RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]
My apologizes if this has been answered but I haven't found anything that works for me. This is on a cheap godaddy shared server, php based.
I have tried a few RewriteRules but to no avail.
Basically, if you are linked to something like :
www.example.com/items/apple
It would keep that in the url, but instead go to:
www.example.com/items
In the items page, I would be able to get the /apple part and do with it as I will. Either having it passed in through parameter like ?dir=apple or just as the /apple. My problem right now is the redirecting. My .htaccess file looks like this
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
Try adding an additional condition and/or switch the order:
RewriteEngine On
Options -Multiviews
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
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]
Currently I have the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
Which works allmost perfect.
It rewerites urls like http://domain.com/something/ to the public/index.php file, like a charm, except when it is a file, just like it should.
However http://domain.com (without any path appended) (there is no index.php in the root, so it gives a 404 at the moment) is not being rewrited, how can I change this .htaccess so it rewrites this url too?
The index file is in public/index.php I want it to load that file through the use of .htaccess
Thanks
I believe to rewrite the root, you can simply do something along the lines of:
RewriteRule ^$ location/of/root/file [L]
You could try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php
RewriteBase should prepend the rule pattern with a leading slash, forcing it to match the root path.
Untested!
What you have there is inspired by WordPress?? It's a bad idea as it tell Apache to always check if the path is a file or a directory before redirecting.
I have something like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(css|images|javascript)(.*) [NC]
RewriteCond %{REQUEST_URI} !\.(swf|ico|php|xml)$ [NC]
RewriteCond %{REQUEST_URI} !robots.txt
RewriteRule (.*) index.php?page=$1&%{QUERY_STRING} [PT]
The first condition restricts this redirect from working in specific folders.
The seconds does it for specific extensions.
You can guess what the third does :)