Jump to content

PHP application url structure problem


snrecords

Recommended Posts

Hi all,

 

So I'm attempting to create my own Search Engine Friendly website structure, but came across a problem:

 

$current_url = $_SERVER['REQUEST_URI'];

$bb = explode('/', $current_url);

$page = $bb[1];

 

Whatever $page equals, I have an include statement pointing to that file.  I already fixed the .htaccess to accommodate this. 

 

The problem is .... if somebody types in www.example.com/login/adsfasdf

 

then it still points to the right file as www.example.com/login does and the page still works and shows.  How do I fix the code so that www.example.com/login/anytherehere/orhere won't work??

 

Now I can't use

if (count($bb) < 3){blahblahbalh}

... because some of my urls have pagination which requires the url to have a second value in the $bb array.

 

Any ideas?  I'm completely stuck on this?  Alternatives?

Link to comment
https://forums.phpfreaks.com/topic/141558-php-application-url-structure-problem/
Share on other sites

thanks thepip3r, but I'm very familiar with url rewriting with the htacess file.

 

Let me take another crack at explaining by showing more code ...

 

$current_url = $_SERVER['REQUEST_URI'];

$bb = explode('/', $current_url);

$page = $bb[1];

include('somedirectory/'.$page.'.php');

 

And in my htacess code I have

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)$ index.php?page=$1  [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1? [R=301,L]

 

So if someone typed in the url www.example.com/login ... then the login.php is included and shows. 

 

Now if someone types in the url www.example.com/login/asdfasdf ... then login.php still shows

 

.... what I want to do is have the url www.example.com/login/asdfasdf show a  404 error.

 

Now the catch is this ... sometimes I'll need the $bb array to have more than 1 value.  For example, www.example.com/results/2  needs to work, so I can't use the following code to fix this problem:

 

if (count($bb) < 3){show page} else {don't show page}

 

any ideas?

i'm not that familiar with URL rewriting but reading up on it as I have, it sure seems like you should be able to accomplish what you're looking to do with it.  i understand that all paths/variables passed still redirects to the same page even though there are more variables passed than intended.  it sounds like you either need a catch-all RewriteCond that redirects to a custom error page or more specific ReWriteConds from your current .htaccess to more granularly control the paths passed.

 

http://corz.org/serv/tricks/htaccess2.php

 

 

I'll take another crack at this as I was looking at my own URL Rewrites in my .htaccess.

 

if your problem is that people are passing extraneous information (potentially) and you want to be able to trap for it, why not just use (if/then/else or switch) statements to check the $_GET['page']  element? 

 

Again I apologize for not being a pro at URL rewriting but if your rewrite is only expecting:

 

www.example.com/login/

 

and you're getting

 

www.example.com/login/askasdf

 

...couldn't u just do something like:

 

 $pos = strrpos($_GET['page'], "/");
if ($pos === false) { 
    // no backslash found -- valid argument
} else {
    // backslash found -- include some error page, code, or redirect to error page etc
}

 

HTH a bit more??

 

edit:  clarified comments in code

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.