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.
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.
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 just started .htaccess so I do not know too much about it, I try to google it but failed so I post my question here, I am sorry if the question is silly for you. My question is.
I use this code in .htaccess page:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
when I enter this url:
http://localhost/learn/php/htaccess/page/number
it works fine. but I also want to add index.php (page name) in the url. I tried this:
http://localhost/learn/php/htaccess/index.php/page/number
AND
http://localhost/learn/php/htaccess/index.php?page/number
but failed. How can I do this?
I simply want to do that if I add another page then I am not sure but I have to use this code:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
RewriteRule ^(.*)/(.*)$ another.php?x=$1&y=$2
but how can I open the another.php page with this format?
thanks
Try adding a few conditions to your rules:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
As for routing to another page, you'll need to distinguish the difference between the two routes. For example, given a url:
http://localhost/learn/php/htaccess/foo/bar
where should it get routed to?
index.php?n=foo&p=bar
or
another.php?x=foo&y=bar
?
htaccess knows nothing about the content, only the URL and a regex pattern to match it, so unless you differentiate between the two, you can't route the same thing to two different scripts.
You can just make it easy like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?data=$1 [QSA]
in your index.php you put this code
$data = explode("/",$_GET['data']);
$page = $data[0];
$number = $data[1];
I am working on urls of a site. I want to convert the following url into
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
this url
http://dev.steelogic.com/solution/metalbuildingproduct
this is the htaccess code I am using.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/(.+)$ $1.php?id=$2
I don't know anything about htaccess. usually I just copy paste the text and it works.
the url with ?id= works fine
but when try to use the 2nd url it doesn't have any id.
I have to do this to many other pages as well and the all contain [.php?id=] which I want to replace with [/] slash
Try this rule in your DocumentRoot/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?id=$2 [L,QSA]
Please try this:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteBase /
RewriteRule ^solution/([^/.]+)/?$ solution.php?id=$1 [PT,L,NC,QSA]
</IfModule>
Because you say that even without this rule, going to http://example.com/solution/asdf loads /solution.php on your server, something else must be working on your url. Because you say you didn't define such a rule, it must be coming from somewhere else. Either a rule is defined somewhere else (main config file) or MultiViews is enabled. If you can't apply anubhava's solution (disabling MultiViews), move your file somewhere else, for example under /public/solution.php, so that url never partly matches an existing file, then use the following rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f #partly copied from anubhava's answer
RewriteRule ^([^/]+)/([^/]+)/?$ /public/$1.php?id=$2 [L]
from all of the discussion I concluded that its difficult enough to do it with .htaccess as there might b some sort of module working on the urls of site from the hosting and I get both solution and solution.php directed to same page
so I did it in php for now.
as both of the following pages
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
and
http://dev.steelogic.com/solution/metalbuildingproduct
being going to same pages but with the second url I was not getting query string var. so I wrote a PHP code as I knew on each page how many vars I need so it worked for me. although it may not be a good approach but it works
$URI = explode("/", trim($_SERVER['REQUEST_URI']));
$numElems = count($URI);
if ($numElems <= 4) {
$arr = array();
$arr['id'] = $URI[2];
if (!empty($URI[3]))
$arr['id2'] = $URI[3];
}
if any body can help me with .htaccess I will appreciate.
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.