Danielx64 Posted March 13, 2016 Share Posted March 13, 2016 Hello all, Just wondering if I could get some help here. I'm almost finished with building my CMS system, and something that I been trying to get working is that if someone use the wrong url format, it would do a 301 redirect and carry on. I am struggling to work out what I'm doing wrong. When $config['blog_seo_enabled'] is set to on and I go to http://localhost/?p=test the url would change to http://localhost/test that is expected. When I turn $config['blog_seo_enabled'] off, and I head to http://localhost/test I am getting redirected to http://localhost/?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=test with firefox throwing up "The page isn't redirecting properly" when I should be getting http://localhost/?p=test. $page_url = request_var('p', ''); $page_url_rewrite = substr(rawurldecode($_SERVER['REQUEST_URI']),1); if (preg_match('/^index\./', $page_url_rewrite)) { // The user probably wanted the home page header('HTTP/1.1 301 Moved Permanently'); redirect($config['server_protocol'].$config['server_name'].'/'); } if($page_url && $config['blog_seo_enabled']) { //echo($config['server_protocol'].$config['server_name'].'/'.$page_url); //exit(); // If seo links are enabled and someone trying to access via /?p=* we redirect then to the correct page (/*) header('HTTP/1.1 301 Moved Permanently'); redirect($config['server_protocol'].$config['server_name'].'/'.$page_url); } elseif($page_url_rewrite && !$config['blog_seo_enabled']) { // If seo links are not enabled and someone trying to access via /* we redirect then to the correct page (/?p=*) header('HTTP/1.1 301 Moved Permanently'); redirect($config['server_protocol'].$config['server_name'].'/?p='.$page_url_rewrite); } Could someone please let me know how I should be doing it? My .htaccess looks like this RewriteEngine On RewriteRule ^a/([0-9\-]+)/?$ index.php?b&a=$1&seo=1 [QSA,NC] RewriteRule ^s/\??author_id=([0-9]+)$ index.php?b&author_id=$1&seo=1&search [QSA,NC] RewriteRule ^s/(.*)$ index.php?b&keywords=$1&seo=1&search [QSA,NC] RewriteRule ^resources/([0-9]+)/(.*)$ index.php?downloadfile&i=$1&checkseo=$2 [QSA,NC] RewriteRule ^rss/([a-z]+)\.html$ rss.php?mode=$1&seo=1 [QSA,NC] RewriteRule ^rss/([a-z0-9-]+)/([a-z]+)\.html$ rss.php?b&ci=$1&mode=$2&seo=1 [QSA,NC] RewriteRule ^c/([a-z0-9-]+)/$ index.php?b&ci=$1&seo=1 [QSA,NC] RewriteRule ^c/([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?b&cp=$1&ci=$2&seo=1 [QSA,NC] RewriteRule ^([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&p=$2&checkseo=$1 [QSA,NC] RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&ci=$1&p=$3&checkseo=$2 [QSA,NC] RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&cp=$1&ci=$2&p=$4&checkseo=$3 [QSA,NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] Thank-you Quote Link to comment https://forums.phpfreaks.com/topic/300988-redirect-loop-when-seo-mode-is-off-and-redirecting-to-correct-path/ Share on other sites More sharing options...
requinix Posted March 13, 2016 Share Posted March 13, 2016 The REQUEST_URI includes the query string too. $page_url_rewrite will be either "test" or "?p=test". Dump out $_SERVER and look for a more appropriate value to use... Quote Link to comment https://forums.phpfreaks.com/topic/300988-redirect-loop-when-seo-mode-is-off-and-redirecting-to-correct-path/#findComment-1531962 Share on other sites More sharing options...
QuickOldCar Posted March 13, 2016 Share Posted March 13, 2016 I'm not sure what your redirect function does. If you use header(), do an exit; after so the rest of code does not continue. Seems to me it's doing this rule constantly and passing empty p parameters. Here is... $page_url_rewrite = substr(rawurldecode($_SERVER['REQUEST_URI']),1); $_SERVER['REQUEST_URI'] will hold the full request path including the query string redirect($config['server_protocol'].$config['server_name'].'/?p='.$page_url_rewrite); Going by your rules will add the p parameter and then also any directory plus additional query string $page_url = request_var('p', ''); //what is request_var() function doing? $page_url is defined as being set and blank, why add this if not using it? The $_GET or $_REQUEST array contains the parameters and values current in the url. Quote Link to comment https://forums.phpfreaks.com/topic/300988-redirect-loop-when-seo-mode-is-off-and-redirecting-to-correct-path/#findComment-1531963 Share on other sites More sharing options...
QuickOldCar Posted March 13, 2016 Share Posted March 13, 2016 I may as well mention http_build_query() to properly build the query string. 1 Quote Link to comment https://forums.phpfreaks.com/topic/300988-redirect-loop-when-seo-mode-is-off-and-redirecting-to-correct-path/#findComment-1531964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.