how to set right RewriteRule for epmty query - php

This is my .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^(.*) data.php?chn=$1 [L,QSA]
I have 2 php files.
index.php and data.php
I to want call index.php when the request is for domain.com/ and data.php when the request is for domain.com/(anything) .

To reroute from index.php...
if (!empty($_GET['chn'])) {
require('data.php');
exit();
}
data.php will already have $_GET['chn'] available to it.
or if you aren't queuing off a get var then you can check the REQUEST_URI and do the same thing:
if (#$_SERVER['REQUEST_URI']!='/')) {
require('data.php');
exit();
}

RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/(\d+)*$ ./data.php?chn=$1
Try this.
Note: When using .htaccess in your desired case, placing / after the domain name will trigger the second RewriteRule case, meaning user will try to open literally domain.com/data.php?chn=.

You need to change your regex pattern from (.*) to (.+) to avoid rewriting your homepage otherwise the request for / will also be rewritten to data.php :
Try this rule :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/data\.php [NC]
RewriteRule ^(.+) data.php?chn=$1 [L,QSA]

its work ! thank you all using this code :
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ data.php?chn=$1 [L,QSA]

Related

redirecting a php url with 2 parameter

I am trying to set up my api to have 2 endpoints, and the simplest way I have seen to do this is to have a .htaccess file that would redirect something like www.website.com/api/category/platform to display the same as www.website.com/api/index.php?category=platform . The issue I am now running into is that I want it to include a second parameter such as www.website.com/api/index.php?category=platform&filter=price to be the same as www.website.com/api/category/platform/price or even www.website.com/api/platform/price
The current .htaccess file is
RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
I figured it out and I was able to use something along the lines of
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ project3/api/index.php?category=$1&filter=$2 [L]
You can use smthing like this.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?category$ $1category/ [R=301,L]
RewriteRule ^ - [L]
RewriteRule . index.php [L]

Passing URL with htaccess

htaccess below diverts PDF calls to validate.php where name of the PDF is get with $_GET['filename']
Problem is, it works with URLs below but it doesn't when there are many sub directories so what I need is, instead of just getting name of the file, I should pass whole URL to validation.php something like ^(.*)$ /validate.php?request_url=$1 [L] (this is just an example).
Note: instead of whole URL, anything after domain is acceptable.
WORKS:
http://www.whatever.com/1.pdf
http://www.whatever.com/2.pdf
http://www.whatever.com/3.pdf
WON'T WORK:
http://www.whatever.com/sub1/1.pdf
http://www.whatever.com/sub1/sub2/1.pdf
http://www.whatever.com/sub1/sub2/sub3/1.pdf
How can I do it?
Thanks
CURRENT HTACCESS:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^(.*)$ /validate.php?filename=$1 [L]
CURRENT VALIDATION.PHP
if (isset($_GET['filename']))
{
//Do something with it
}
else
{
//Don't do anything
}
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^ /validate.php?filename=%{REQUEST_URI} [L]
Try this:
RewriteRule ^(.*)\.pdf$ /validate.php?filename=$1 [L]
without the RewriteCond

RewriteRule not redirecting

Due to request, every page needs to be prefixed with "index.php"; if the path was first /it, now it needs to be /index.php/it.
I have tried changing base url in settings.php. But it doesnot works.Tried rewriting in .htaccess also. I have tried the following code
RewriteBase /
RewriteCond %{HTTP_HOST} ^http://www.example.com/index.php [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php [L,R=301]
But it doesnot works. Somebody help me please. Thanks in advance..
Make sure you have mod_rewrite enabled and try with this
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://www.example.com/index.php/$1 [L,R=301]
Remember that the $1 refers to the (.*) you have captured in the URL
You are not using your capture group (.*). You can refer to it using $1
RewriteRule ^(.*)$ http://www.example.com/index.php/$1 [L,R=301]

.htaccess rewrite to produce clean url

Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url

i need some help creating Htaccess

i need to hide the extensions of my webpage and also want to let the user put the links in both (lower and upper) case:
Example:
the file name is demo.php
www.example.com/demo
www.example.com/DEMO
www.example.com/Demo
Running PHP in a LAMP server, no access to php.ini, just .htaccess
Actualy im using a file like this:
RewriteEngine On
RewriteBase /
RewriteRule ^/(OUTSOURCING|outsourcing|Outsourcing)$ outsourcing.php [NC,L]
And i m reciving this error:
Not Found
The requested URL /outsourcing was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
RewriteEngine On
RewriteBase /
www.example.com/DEMO www.example.com/Demo or doing www.example.com/DEMO/page2
RewriteRule ^/(DEMO|demo|Demo)/(.*)$ demo.php?=$1 [NC,L]
RewriteRule ^/(DEMO|demo|Demo)$ demo.php [NC,L]
RewriteRule ^/(D|d)emo$ demo.php [NC,L]
or pass anything www.example.com/DeMo www.example.com/bob to demo.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ demo.php [NC,L]
you may want to test if your allowed .htaccess RewriteRule /*$ http://google.com [R][L]
here is a good way to do it with case insensitive method
EDIT:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${lc:$1}.php [NC]
this way anything entered will be redirected to a php file.
edit : this way your js and css file can still run
RewriteRule ^/[Dd][Ee][Mm][Oo]/?(.*)$ demo.php [NC,L]
will redirect any capitalization of "Demo" to demo.php
First add this line in the <VirtualHost> section OR at the end of your httpd.conf file (to enable lc function in .htaccess for later use):
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond ${lc:%{REQUEST_FILENAME}}.php -f
RewriteRule . ${lc:%{REQUEST_URI}}.php [L]
First rule in .htaccess is doing external redirect by making a URI of /index.php to /index
Second rule in .htaccess is doing internal redirect by makign a URI of /INDEX to /index.php by lowercasing the URI. Assuming you have filename index.php physically present.
That way you can always write URLs without .php extension.

Categories