Aphace .htaccess Routing Assistance - php

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.

Related

URL: How to make get parameters looks like directories

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.

PHP and .htaccess doesn't allow $_GET

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.

add page name in the url using .htaccess

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];

How to rewrite URL using htaccess for clean url (remove unnecessary parameter from url)

I am working on localhost for PHP project. I need some solution.
Current URL : http://localhost/project/portfolio/php_files/port_sub_sidebar.php?x=opencart
Now, i want to show in URL like this: http://localhost/project/portfolio/opencart
How's that possible doing change in .htaccess file
In .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(.*)(\.jpg|\.gif|\.png|\.css|\.js)$
RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]
you can get the url by the code below
$url = $_GET['url'];
$segments = explode("/",$url);
print_r($segments);
then you can implement your routing system based on the provided segments

Clean URL with htaccess. no query string variable received

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.

Categories