How to remove .php extension the pro way - php

I have created my first Php solution but have trouble removing the .php extension from the URL. It works in the root but not in sub-folders?
Eg www.domain.com/index.php reached from www.domain.com/index (or www.domain.com/)
Then it gets tricky.
For example. www.domain.com/en/europe/bikes/racer-bike.php will not differ from www.domain.com/en/bikes/racer-bike without ending up on www.domain.com/en/bikes/ and on index.php
My .htaccess file looks like this:
Options -MultiViews
RewriteEngine on
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# SSL
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R=301,QSA]

I Try it and it works:
My dirs structure
/
index.php
/sub
file.php
And my htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^\/]*)$
RewriteRule ^([^\/]*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([A-Za-z]+)/(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^([A-Za-z]+)/(.*)$ $1/$2.php [NC,L,QSA]
Try it

You are doing it wrong ... Mostly you do not make php files for each uri (route). You should not remove php extension you should direct all uri's to index.php and use some router to define how to handle each request. or you can just use some framework to do that like laravel or symfony etc.

Related

Get URI params without index.php

I have a URL like http://example.com/index.php/fruit/1a2b3c
I want to get the URI's for which I have written a code.
Now I want to remove the index.php from the visible URL, it should work even when the URL is http://example.com/fruit/1a2b3c and it should still point to index.php
I am on apache2 using PHP 7
Add following code to your .htaccess file.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
change your .htaccess file to,
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
hope this will work.

Redirect to a subfolder using htaccess

I have searched all the similar topics in the forum and nothing helped me. Here is what I am looking for:
I have a site having a login url as
site.com/folder1/php/login.php
Now I want to hide the "php" folder name from url, also remove .php extension so that it will look like
site.com/folder1/login
Also the folder name "folder1" may vary, but the folder name "php" remains the same in all urls.
Try this in your Root/.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/?([^/]+)/([^/]+)/([^.]+)\.php [NC]
RewriteRule ^ /%1/%3 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /folder1/php/$2.php [NC,L]
This should be working.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+php/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^php/)^(.*)$ /php/$1 [L,NC]

.htaccess rewriterule folder and files rewrite

I have problem with my rewrite rule. I have new webpage in root/hrp. and if i open pages in that directory, then all is OK, but when i wanna surf those pages from root (without /hrp/) then is problem. I found some .htaccess rules and all working fine, but if i wanna execute some pdf,jpg,or php script directly, then come problem.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule ^(.*)$ /index.php [L]
if i put this bottom code on begining, i can open files directli, but pages don't work and vice versa
RewriteCond %{REQUEST_FILENAME} (/|\.php|\.html|\.htm|\.png|\.jpg|\.jpeg|\.gif|\.xml|\.rss|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule ^(.+)$ $1 [L]
Try adding this at the top instead:
RewriteRule \.(php|html?|png|jpe?g|gif|xml|rss|feed|pdf|raw)$ - [L,NC]
or you can try adding this condition to your hrp rule:
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -d
RewriteRule (.*) /hrp/$1 [L]
I have find solution
don't rewrite this files
RewriteCond %{REQUEST_URI} !.(gif|jpg|png|ico|css|js|swf|wav|mp3|less|cur|pdf|jpeg|txt|ico|zip|gz) [NC]
don't rewrite this folder - there i have php files, and dont rewrite this files
RewriteCond %{REQUEST_URI} !(folder1|folder2|file1.php|file2.php|file3.php) [NC]
all content is in "/hrp/" folder
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
but url don't contains "/hrp/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule (.*) /index.php [L]

Remove /index.php/ part of CodeIgniter URLs

I tried to get rid of "index.php" in url using the following rewrite code.but it's not working
please help me to fix this bug
# Development
RewriteEngine On
RewriteBase /mvcTestApp/blog/ciBlog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
In .htaccess file, add the following.
#Rewrite index.php
#Start using rewrite engine
RewriteEngine On
#Rewrite condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Whenever index.php is there in the url, it will rewrite to / automatically
RewriteRule .* index.php/$0 [PT,L]
Check this link for more detail: http://subhra.me/remove-index-php-urls-codeigniter/
This htaccess is working for me.Try this.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Use this rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
missing "/" before "index.php" in last rule
or refer URL
http://ellislab.com/codeigniter/user-guide/general/urls.html
Here's my suite of rewrite rules for CodeIgniter. Please read the inline comments:
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Remove the "index.php" part of URI
# Remove access to "codeigniter" and "data" folders
RewriteCond %{REQUEST_URI} ^(codeigniter|data).*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Block access to "hidden" directories whose names begin with
# a period. e.g. .git, .svn
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
# WWW redirect
RewriteCond %{HTTP_HOST} ^(?!www\.).*
RewriteCond %{HTTP_HOST} ^(?!dev\.).*
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the root index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Creating a Pretty URL

It almost is similar to how to create pretty urls but, it does not seem to work for me.
Here is my url:
http://localhost/pr/ajax/ajax_load.php?task=get_blob&blid=199
How do I convert it to:
http://localhost/pr/ajax/get_blob/199
Here is my .htaccess:
RewriteEngine On
RewriteBase /pr/ajax/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+pr/ajax/ajax_load\.php\?task=([&\s]+)&blid=([0-9]+) [NC]
RewriteRule ^/%1/%2? [R=301,L,NE]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /pr/ajax/ajax_load.php?task=$1&blid=$2 [L,NC,QSA,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|gif|png|pdf)$ /pr/ajax/%{REQUEST_URI} [NC,L,R=302]
Where should the .htaccess be placed? I mean, does it matter where you put it at? My root is at pr.
The location of the .htaccess file is important, specially for the URI-path tested in the rewrite rule, that contains only the path segment after the directory where the file is located. To overcome that limitation, only the REQUEST_URI variable is used in the following code.
Here is a tested solution that works with .htaccess file at either /pr or /pr/ajax/ directories:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /pr/ajax/ajax_load\.php\?task=([^&]+)&blid=([^&\s]+) [NC]
RewriteRule .* /pr/ajax/%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !ajax_load\.php [NC]
RewriteCond %{REQUEST_URI} ^/pr/ajax/([^/]+)/([^/]+)/? [NC]
RewriteRule .* /pr/ajax/ajax_load.php?task=%1&blid=%2 [L,NC]
Try this:
RewriteEngine On
RewriteRule ^/pr/ajax/(.*)/(.*)$ /pr/ajax/ajax_load.php?task=$1&blid=$2 [NC,L]
And yes, it does matter where you put the htaccess file. You put the file in the directory of the file whose path you want to modify/manipulate.

Categories