0

My htaccess file contains this

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php

this redirects all requests to my index.php page, which includes different files as required. That file brakes the url on the basis of / and gives output as an array.

$request = split("/", $request);

If someone accesses mywork/yash , the query works fine as $request[0] becomes yash. But when I program for $request[1] it gives a undefined offset error.

I know this error arises when an array index which is undefined is accessed, but when mywork/yash/ is acccessed, the error doesn't occur anymore !.

Can someone tell me the .htaccess rule to add a tail-ending slash after every request so as soon as the user types mywork/yash it gets converted to mywork/yash/.

Thanks in advance !

3
  • The URL path will always begin with a /, so this doesn't make sense. You will never see mywork/yash as a path. It will always be /mywork/yash. Did you miss some important details in your question? Jan 22, 2016 at 18:11
  • Where is $request coming from? And what does it contain? (It obviously doesn't contain mywork/yash.) split() is deprecated, you should be using explode() instead. The problem would seem to be with your processing of $request, not with a trailing slash - the two are unrelated?
    – MrWhite
    Jan 22, 2016 at 21:16
  • it would contain yash . I want to add tail ending / to all links which user writes in the addressbar Jan 23, 2016 at 5:33

2 Answers 2

0

The trailing slash

Using a rewrite rule to add the trailing slash is possible, but not purposeful since in that case, you can't redirect to index.php anymore.

If you really need that slash, you can still add it in your PHP script.

Splitting the request

Assuming that $request is derived from $_SERVER['REQUEST_URI], you can split it at every occurence of / by writing

$parts = explode('/', $request);

For mywork/yash it would be

array(2) {
  [0]=>
  string(6) "mywork"
  [1]=>
  string(4) "yash"
}

And for mywork/yash/ it would be

array(3) {
  [0]=>
  string(6) "mywork"
  [1]=>
  string(4) "yash"
  [2]=>
  string(0) ""
}

But when using $_SERVER['REQUEST_URI], you will always have a leading slash, which will result in the 1st element of parts being an empty string. You should be able to easily sort empty strings out, though.

1
0

Finally found a solution !

made a function which solves like a breeze !

function redirect_to_unslashed($i)

{

if(isset($_SESSION['request'][$i]))

{

if($_SESSION['request'][$i] == '') { navigate($_SESSION['request'][$i-1]);
}
}

}

this function actually redirects if the last slash contains an empty string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .