I have a php project using a directory structure and I want to create pretty urls.
Basically I want to remove ".php" extension of each file and delete path stuff between localhost and the filename.
Example:
transform this => localhost/myproject/views/pages/login/login.php
into this => localhost/myproject/login
I know I have to configure .htaccess mod_rewrite but I don't know how exactly to be honest.
Try and add this to your .htaccess file:
RewriteEngine On
RewriteBase /
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]
It should basically remove all the .php extensions from being showed.
Okey, I've solved it. I have a index.php file in the root folder so any url will redirect to that file adding RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] in .htaccess using a query string with the view name I want to show. Then I had to rewrite all links to look like "/myproject/name_of_the_view" inside of the project
Related
Here's my situation: I have a directory which I need to rewrite URLs in, so that ?page=hello can be accessed at either /hello or /hello/. I then have a sub-directory which needs .php extensions to be internally appended, so /subdir/example.php can be accessed at /subdir/example and /subdir/example/.
I developed the two parts of the site independently, and I'm now having trouble getting htaccess to meet both requirements.
My current .htaccess file is:
RewriteEngine On
# The root directory bit...
Redirect 301 /home /
RewriteEngine On
RewriteCond %{REQUEST_URI} !/subdir
RewriteRule ^([A-Za-z0-9-_/]+)/?$ index.php?page=$1 [L]
# Remove .php
RewriteEngine On
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
The rules work perfectly independently, ie if I remove the other one, but, together, does append.php but the rewrite rule fails.
I've tried making two separate htaccess files, one for the subdir and one for the root dir, with the appropriate rule (nice URL rule for root dir and remove .php for subdir), but this just means that appending .php no longer works.
Does anyone have any solutions to this?- or any resources to point me in the right direction? I'm currently in a hole of PHP and Apache documentation!
This is what worked:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(subdir/.*?)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
This does only remove .php for files of the subdirectory, but is certainly better than nothing.
I have two questions im struggling with :
I have a static web pages in the following directory of my apache :
var/www/folder/
I have managed to redirect my domain to the above folder to load the html pages, however the when I go to the domain the url shows domain.com/folder Now I understand I need to change the sites-available in the etc/apache2 folder in order to directly hit the domain.com however im trying to achieve it without having to change the etc/apache2 sites-available. Is there a way?
The reason I do not want to change the sites-available is because there is a folder2 inside www which I need to access at some point. How do I redirect the domain without showing the folder on the url?
Inside the www/folder/ I have a few php pages, Now I have tried using .htaccess in the www and inside www/folder/ to remove the .php from the url, however it doesn't seem to work. How do I remove the .php extension from the url?
Thanks in advance.
If you want to remove the folder name from the URL then you should check this link. It has almost same issue, that you are facing.
how to remove folder name from url using htaccess
Beside this to remove .php Extension use this :
Option 1 :
RewriteEngine On
RewriteBase /
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]
Option 2:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
I have the following in my .htaccess which basically removes .php extensions from url and redirects user to the url without the .php extension. However, if i have a querystring for lets say account.php, it will be account?user=john&id=12, I would like my htacess code below to be abe to let convert the querystring to forwardslashes.
Any ideas how I can do this? Thanks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
Create a .htaccess file with the code below
RewriteEngine on
RewriteRule product-categoryid-(.*)-productid-(.*)\.htm$ Your Root URl?categoryid=$1&productid=$2
Your Root URl
Root URL means like wwww.google.com/
I have very poor knowledge about .htaccess
Goal:
Param can be set before index like: www.example.com/param/index
. Param also can not be set (www.example.com/index).
In php i need get this param something like $_GET['param']
Orginal file structure are: www.example.com/index.php
Note. Index.php can by any file
Now i have
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
How to remove .php extension at filename end. How to continue this?
www.example.com can by any http, https, non-www, www domain
If I interpret the question correctly, you can use these rules in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php?param=en [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$2\.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ $2.php?param=$1 [L,QSA]
I have folder called Pages which contains .php files. I want to convert .php extention to .html for this particular folder only.I have the .htaccess code to convert the extention
RewriteRule ^([a-z0-9_]+)\.html$ /index.php$1 [NC,L]
But I wanted to make changes for the Particular folder only.
Any ideas???
The .htaccess file relates to the folder in which it placed in and to all the other subfloders.
Therefore, if you want to apply some rewrite rules only to one folder, you need to put an htaccess in that specific folder.
it will change all php files to html files like foo.php to foo.html
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
You do this by specifying the folder name after the RewriteBase line in your .htaccess as shown below.
RewriteEngine On
RewriteBase /sampleFolder/
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
convert .php extension to .html extension in subdirectory using .htaccess file (convert .php extension to .html extension for all files in particular folder using .htaccess file)
RewriteEngine on
RewriteBase /subdirectory name
Example
RewriteEngine on
RewriteBase /gallery
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]