Give param before filename with .htaccess and serve to php - php

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]

Related

URL rewrite guide

my url is like below :
http://localhost/project/test/jobs/?page=accounting
Accounting
And i want to make like below :
http://localhost/project/test/jobs/accounting
i have search other question but unable to make it. As i am totally unaware about this .htaccess rewrite process :(
however i have below code already in my htaccess file to remove .php extension :
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
please help me to append URL rewrite rule in above htaccess file without loosing .php ext rule.
you are making get request with params which remains as it is if u wana make post request then it will be somethong like this
http://localhost/project/test/jobs/accounting
What I normally do with link building is sending all my traffic to my index.php whitch splits it into a MVC framework, this is the important part of my .htaccess for you:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1 [L,QSA]
What does this .htaccess do rule by rule?
Options +FollowSymLinks
http://www.maxi-pedia.com/FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Checks or the requested file isn't a direct path to a directory or actual file, correct me if I am wrong, because I am not 100% sure.
RewriteRule (.*) index.php?path=$1 [L,QSA]
Rewrites the path from "/test/testings/lalalala" into "index.php?path=/test/testings/lalalala".
I hope that this was the answer where you were looking for.

htaccess mod-rewrite: link to subdirectory from root - neglect dirs in subdirectory?

I asked part of this question a while ago on stackoverflow and this works perfect:
es
I have a multi-domain-server each directory is correspondend to a domain-name. However my main-domain name www.mydomain.com is also a directory on my server and not linked to automatically.
This is why I use this in my root of the website …
root/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
Now when I call www.my-website.com it automatically runs the /my-website directory. That's just fine. However in this directory I have another .htaccess file with this in it.
root/my-website/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^/jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(/downloads/.+)/?$ assets/$1 [L,NC]
So this means that for this website some links like /projects/ are just "neglected" and don't actually exist.
However if I run this website now live on my server and due to the main-htaccess file in my root, the RewriteConditions in my sub-directory don't work and I get a "Not found"
How can I make this work together?
Have it this way:
DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
/my-website/.htaccess:
RewriteEngine On
RewriteBase /my-website/
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/my-website/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I was posting this on serverfault, when question was deleted. You already have a working answer, but I thought I'll still post this here, so you can have some explanations about this:
When you have mod_rewrite instructions in a .htaccess in a subfolder, only those instructions will apply, those of the parent folder will not be applied. You can make the rules that are in the .htaccess of the parent folder apply by using the
RewriteOptions Inherit
directive in the subfolder. But then it could become a bit messy. And you need to remember that the rules in the parent folder are applied after the rewrite rules in the subfolder. Better you go with a solution like proposed in only 1 .htaccess file.
Your 3 last RewriteRule search for a "/" at the beginning, which they never have in a per directory context, but have in a per server context. So if you are in .htaccess file (per dir context) then the comparison is made with a string that has no / at the beginning. See the doc about the RewriteRule.

.htaccess redirect to index.php in any directory

i have been working on a framework that i am creating in php and the essential part for it to work is to send all the url queries to index.php in the root folder.
I managed to do it in the folloowing code at my .htaccess file
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to index.php
#RewriteRule ^(phpmyadmin)($|/) - [L]
RewriteRule ^(html)($|/) - [L]
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* /index.php
The problem occurs when i move the site to a different folder for example
www.mydomain.com/subdir/{where the index.php is, along with htaccess}
and it stops working here. The same happens if i move the framework application to a sub-domain as well.
so I am trying to modify .htaccess to rewrite to index.php properly where the .htaccess file is at regardless if it is in a sub-directory or sub-domain. How can i get the .htaccess to know that application is in a sub-directory and rewrite to it properly so change of location does not break .htaccess pointing to the right file?
www.domain.com/{.htaccess+index.php + framework} => works properly
www.subdom.domain.com/{.htaccess+index.php + framework} => does not work
www.domain.com/subdir/{.htaccess+index.php + framework} => does not work //send to www.domain.com/index.php
localhost/projectname/{.htaccess+index.php + framework} =>does not work
As you can see it needs to send the requests to index.php where htaccess is also located at.
You can use this trick to always write in index.php of the current directory:
RewriteEngine On
# Generate BASE dynamically
# It compares REQUEST_URI variable (which is complete path) with the URI matched by
# RewriteRule (which is relative to current path) and gets differential in
$ %{ENV:BASE} variable.
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule ^(html)($|/) - [L]
# Redirect requests to %{ENV:BASE}index.php which is index.php of current directory
# It also makes sure index.php exists in current directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.php -f [NC]
RewriteRule . %{ENV:BASE}index.php [L]
Edit based on new examples:
How about something like this? Basically check to make sure it's not a file, If not a file redirect to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php [PT,L,QSA]

.htaccess not rewriting the URL correctly for .php files, but OK for .html files

I'm trying to accomplish 2 things in my .htaccess:
Redirect all requests for (in example) www.blanklabs.com/boarddrive/faq, www.blanklabs.com/boarddrive/faq.htm, www.blanklabs.com/boarddrive/faq.html, or www.blanklabs.com/boarddrive/faq.php to www.blanklabs.com/boarddrive/faq.php
The browser's address bar should show just www.blanklabs.com/boarddrive/faq, without the extension.
Here is my current .htaccess:
RewriteEngine On
# -- new
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [L,QSA]
On the server I have faq.html (for now), but I also tried having both faq.html and faq.php. Eventually it'll just be faq.php.
The .htaccess is clearly incorrect, since if I go to www.blanklabs.com/boarddrive/faq.html I get the correct content (from faq.html), but if I go to www.blanklabs.com/boarddrive/faq.php I get a 500 error. This happens even if I have faq.php on the server.
Any ideas what I'm doing wrong? The no-extensions is secondary, the primary goal is to redirect all requests from html to php files.
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# skip POST requests
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html?)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)(\.html?)?$ /$1.php [L,QSA]
You need to redirect
/faq.htm
/faq.php
[using redirect directive]
to /faq
now just applying rewrite rule to this later condition [/faq] to
/faq.php
It should work.

.htaccess - Redirect subfolder to index.php using subfolder name as variable

This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]

Categories