I am trying to pass the entire Request_Uri into a $_GET request however the only things it's passing is 'test.php'
Htaccess code
RewriteEngine on
RewriteRule ^(.*)$ test.php?data=$1 [L]
test.php
<?php
echo $_GET['data'];
?>
However the only thing it display is 'test.php' what am I doing wrong?
Use this:
RewriteEngine on
# skips files
RewriteCond %{REQUEST_FILENAME} !-f
# skips directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ test.php?data=$1 [L,QSA]
Without these 2 conditions your rules runs twice since you're matching .* and you get rewritten URI test.php as GET parameter.
Related
I'm tring to implement a htaccess redirect that occour only if there are parameter in the index.php
The result should be:
If I call mydomain.com or mydomain.com/index.php I should see the index.php (normally)
If i call mydomain.com/myvar It should redirect to script.php?var=$1
But if I call other files (example mydomain.com/page.php) or other directroy (example mydomain.com/folder/) I should see these files normally, without any redirection.
Is that possible? This is what I've got so far:
RewriteRule ^(.*)$ script.php?var=$1 [L,QSA]
But it does redirect everything, including /index.php (without params), /page.php and /folder/
You can use the following rule :
RewriteEngine on
#not a file
RewriteCond %{REQUEST_FILENAME} !-f
#not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite the request path to script.php
RewriteRule ^(.+)$ script.php?var=$1 [L,QSA]
I also changed your regex pattern to (.+) to prevent the redirection of your homepage / .
This request:
http://domain.com/newest
should be rewritten to:
http://domain.com/index.php?list=newest
I tried to achieve that redirection with .htaccess and arrived at this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?list=$1 [L,QSA]
But that still doesn't seem to work. How do I get this rewrite and redirection to work?
You have to change the RewriteRule condition in this way:
RewriteRule ^(.+)$ /index.php?list=$1 [L,QSA]
Then, in our script, you can access to rewritten rule through $_SERVER[QUERY_STRING], whereas the original request is available as usual through $_SERVER[REQUEST_URI].
Note that - if your intention is to remap all ‘Not Found’ incoming requests, you can also simply use this rule:
RewriteRule . /index.php [L,QSA]
and then, at the top of index.php file, process the $_SERVER[REQUEST_URI] and assign the proper value to $list variable.
So, i.e., having URL http://domain.com/newest:
$list = substr( parse_url( $_SERVER['REQUEST_URI'])['path'], 1 );
$list will assume newest as value.
I'm in the process of overhauling one of my projects, a web based booking system, but I'm having a bit of an issue with my htaccess file. The old system was pretty standard, with .php scripts in the route of the website, I had a rule hiding my extensions, and I resultantly had a URL like /viewinvoce?ID=1. I've been trying to find out how to rewrite this URL so it looks a lot neater - in the format /viewinvoice/1, and I'm getting there, but I have a slight problem...
The URL /test works - it adds a trailing slash making the URL /test/, and the value 'test' is passed to the webpage.
The URL /test/ works as above, a trailing slash isn't added since it already exists, and 'test' is passed to the webpage.
The URL /test/1 also works, 'test' and '1' are both passed to the web page,
but when a slash is type after 1 (/test/1/) the page throws a 404.
My .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?PID=$1&ID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])/(.*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
My simple PHP script..
<?php
echo $_GET['PID'];
echo '<br>';
echo $_GET['ID'];
Ideally, I'd like the .htaccess file to add a second trailing slash to the second variable passed, but I'm a bit confused at this point, and ideally a second pair of eyes would be useful!
Try these rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?PID=$1&ID=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?PID=$1 [L,QSA]
Make sure to test it after clearing your browser cache.
The task seems very simple:
all requests must be passed through one file.
However,
when following code is added to httpd.conf:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?route=$1 [QSA,R=301,L]
and following code is added to /index.php:
if( isset($_GET["route"]) && $_GET["route"] != "/index.php" ){
header("Location: ".$_GET["route"]);
}
then it causes redirect loop!
Causes for example such loop:
www.site.com/image.jpg -> (redirected by httpd.conf)
www.site.com/index.php?route=/image.jpg -> (redirected by index.php)
www.site.com/image.jpg -> (redirected by httpd.conf)
www.site.com/index.php?route=/image.jpg -> (redirected by index.php)
...
So, the question is following:
How to stop this loop?
To stop for example in such way:
www.site.com/image.jpg -> (redirected by httpd.conf)
www.site.com/index.php?route=/image.jpg -> (redirected by index.php)
www.site.com/image.jpg (no further redirection)
This one is a bit of patch, but I think it works.
httpd.conf:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^(.*)a=a$ [NC]
RewriteRule ^(.*) index.php?route=$1 [QSA,L]
index.php:
<?php
if( isset($_GET["route"]) && $_GET["route"] != "/index.php" ){
header("Location: ".$_GET["route"].'?a=a');
}
The thing is: First time the rewrite engine redirects the page to index.php. Then, in index.php there's a redirection to the same file but adding parameter a=a. Then rewrite engine comes again, but he has orders of not redirect when found the string a=a in the query string, and there's no further redirection.
Of course this will end in a 404 error, since you're filtering only non-existant files with the first two rewritecond, but I guess you know how to deal with that.
Your rule looks fine but R=301 is a bit odd in your rule as you don't want to expose your internal URL in browser.
However PHP code is looking suspect due to this condition:
$_GET["route"] != "/index.php"
Which will always be true since /index.php itself will not be routed through this rule due to these conditions:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
Hence your code will cause a redirect loop by continuously redirecting using header function.
I suggest keep your rule like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?route=$1 [QSA,L]
And your PHP code should just check presence of $_GET['route'] but should not do any redirects.
First, put this to .htaccess file, not in httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,R=301,L]
Then, remove this redirect from your index.php file. You can get all your variables via $_GET or $_REQUEST.
What a senseless solutions. Of course it will cause redirect loop. In the RewriteCond-s you trying to catch files and dirs names that not exists in the server file system, and then you trying to pass their names to the index.php in which you make to request them again, again and again.
There is a single boilerplate that may come in handy, it should be placed on the top, after RewriteEngine on condition:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !^[\s/]*$
RewriteRule ^ - [L]
An env was added, it works by theory, however I have tested it only once, see if it can solve the issue.
Why are you putting this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?route=$1 [QSA,R=301,L]
In httpd.conf? You need to place this into file called .htaccess, create it into your root directory.
How can I do this that mydomain.com/ -> mydomain.com/profile.php?username=
Now You can start search via url like domain.com/
My htaccess file is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=search&val=$1 [QSA,L]
I can understand in php file
$val = $_GET['val'];
if($c->userNameCheck($val))
{
header("Location:".$url."/profile.php?username=".$val);
}
But I dont want to show profile.php
You could simply replace the rule with:
RewriteRule ^(.*)$ profile.php?username=$1 [QSA,L]
However, since you won't be executing the index.php file, the userNameCheck method won't be called. I assume it does some sort of a check if the provided parameter is a valid username -- to make it run with the modified rule, you'd have to move that call to the profile.php file.