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.
Related
I have the following routing options enabled on my .htaccess file. I want to route everything to the index.php file, yet am expriencing a challenge, because I have a foward slash - '/' being dropped in the process of routing. I need it for me to be able to identify unprovided/empty parameters at URL splitting level.
Options -Multiviews
RewriteEngine On
RewriteBase /example/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
A request like:
localhost/example/public/controller/method/var1/var2//var4
*will be routed as:*
localhost/example/public/controller/method/var1/var2/var4
*An empty var3 was dropped*
I will appreciate that support. Thank you.
The issue seems to be that the additional slashes get removed in the querystring; so when you move them from being part of the URL to being the url parameter on the querystring...
You can avoid this by looking at the REQUEST_URI in the receiving script rather than a $_GET['url'] parameter:
.htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L,QSA]
Then in index.php instead of something like:
$url = $_GET['url'];
... you'd need:
$url = $_SERVER['REQUEST_URI'];
However, with REQUEST_URI you'll get the full path, not just the path after your RewriteBase. Simplest way to remove it would be with str_replace - something like:
$baseURL = '/example/public/';
$replaceCount = 1;
$url = str_replace($baseURL, '', $_SERVER['REQUEST_URI'], $replaceCount);
Put in the $replaceCount just in case you happen to have variables example/public in the URL - it ensures only the first instance will be replaced.
Adding the following rule should work:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
It will replace all double slashes with a single one.
See this post for more information.
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.
I have a .htaccess document in the root of my webserver with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
Everything works perfect I can easily explode the link in PHP using the follinw code:
$params = explode('/', $_SERVER['REQUEST_URI']);
var_dump($params);
Now I want to do a $_GET['name'] request but this isn't working, it is just pasting it in the params array so I am not available to get the code using $_GET.
Do you know how to fix this? The problem is the .htaccess file I suppose.
Maybe you can add the {QUERY_STRING}
RewriteRule ^.*$ ./index.php?%{QUERY_STRING}
This should append all GET params to the new url.
RewriteRule ^.*$ ./index.php [QSA,L]
QSA basically appends query string every time, and L means to stop processing other rules (if any) once this one is matched.
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.
I am trying to pass a variable from one page to another via my url. The structure of the urls looks like this http://localhost:8888/test_portfolio?location=ignite_rockford.
Here's the relevant info from my .htaccess file:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1
</IfModule></code>
The problem seems to be with this line: RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1, but I don't know what to do. If I add .php after test_portfolio, then it works, but otherwise when I dump $_GET['location'] I get NULL.
Oh yeah, I'm retrieving $_GET with $location = $_GET["location"];.
Any help would be appreciated.
If you are just trying to remove the .php, you can just add it and copy the query string with [QSA] tag:
RewriteRule ^(.*)$ $1.php [QSA]
Your URLs will go from:
/test to /test.php?/test?test=test are you sure this is what you want? (it looks malformed, among other things)
Usually you'll write a Router like:
RewriteRule ^(.*)$ router.php?__url=$1 [QSA]
This will give you a GET parameter __url with the original path. [QSA] will allow you to still accept all original query parameters, like location.