snrecords Posted January 20, 2009 Share Posted January 20, 2009 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 More sharing options...
thepip3r Posted January 20, 2009 Share Posted January 20, 2009 If I get what you're asking, I think you simply want something like URL rewriting: http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html Link to comment https://forums.phpfreaks.com/topic/141558-php-application-url-structure-problem/#findComment-740959 Share on other sites More sharing options...
snrecords Posted January 20, 2009 Author Share Posted January 20, 2009 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? Link to comment https://forums.phpfreaks.com/topic/141558-php-application-url-structure-problem/#findComment-740969 Share on other sites More sharing options...
thepip3r Posted January 20, 2009 Share Posted January 20, 2009 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 Link to comment https://forums.phpfreaks.com/topic/141558-php-application-url-structure-problem/#findComment-740975 Share on other sites More sharing options...
thepip3r Posted January 20, 2009 Share Posted January 20, 2009 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 Link to comment https://forums.phpfreaks.com/topic/141558-php-application-url-structure-problem/#findComment-740980 Share on other sites More sharing options...
haku Posted January 20, 2009 Share Posted January 20, 2009 $current_url = $_SERVER['REQUEST_URI']; $bb = explode('/', $current_url); $page = $bb[1]; if(is_file('somedirectory/'.$page.'.php')) { include('somedirectory/'.$page.'.php'); } else { // redirect to 404 page } Link to comment https://forums.phpfreaks.com/topic/141558-php-application-url-structure-problem/#findComment-741007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.