0

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.

RP Jr.
  • 13
  • 4

2 Answers2

0

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.

Community
  • 1
  • 1
Thoby
  • 316
  • 1
  • 6
  • Multiple slashes will be replaced anyway... I think he's trying to keep them to denote an empty var – CD001 Apr 19 '17 at 13:34
0

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.

CD001
  • 8,332
  • 3
  • 24
  • 28
  • This works for me, its easy to follow-through and implement. – RP Jr. Apr 19 '17 at 14:02
  • @RPJr. No worries ;) I noticed it caused some quirks with the `RewriteBase` since you'll get the full path in the `REQUEST_URI` rather than just the path after `example/public` - but if you've worked around that already I'll leave the answer as-is. – CD001 Apr 19 '17 at 14:18
  • the approach i took with the `RewriteBase` was to unset its values within `$_SERVER['REQUEST_URI'];` before passing the rest as parameters. I would appreciate any recommendation. – RP Jr. Apr 19 '17 at 14:55
  • @RPJr. - yeah, a simple string replacement before parsing `$url` would be easiest (and more efficient than `preg_replace()`) - I've updated the answer. – CD001 Apr 19 '17 at 15:10
  • 1
    Sorry for that, i meant to say first I had to explode the `$_SERVER['REQUEST_URI'];` i.e `$parameters = explode('/',$url );`then unset the first two pockets that have the values example and domain before passing the rest as parameter. Thank you. – RP Jr. Apr 19 '17 at 16:06