Rewrite url not working from localhost

I tried to hide my .php extensions with .htaccess at first .. It worked fine after including this in the file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

      

and also include this in my .php function

//==== Strip .php extension from requested URI  
function strip_php_extension()  
{  
  $uri = $_SERVER['REQUEST_URI'];  
  $ext = substr(strrchr($uri, '.'), 1);  
  if ($ext == 'php')  
  {  
    $url = substr($uri, 0, strrpos($uri, '.'));  
    redirect($url);  
  } 
}  

//==== Redirect. Try PHP header redirect, then Java, then http redirect
function redirect($url)  
{  
  if (!headers_sent())  
  {  
    /* If headers not yet sent => do php redirect */  
    header('Location: '.$url);  
    exit;  
  }  
  else  
  {
    /* If headers already sent => do java redirect */  
    echo '<script type="text/javascript">';  
    echo 'window.location.href="'.$url.'";';  
    echo '</script>';  

    /* If java is disabled => do html redirect */  
    echo '<noscript>';  
    echo '<meta http-equiv="refresh" content="0; url='.$url.'" />';  
    echo '</noscript>';  
    exit;  
  }  
} 

      

The thing is, the URL for dynamic pages hasn't changed. Do I still have page.php? p = 5 and blog.php? post = 5 doesn't change to page? p = 5 and blog? post = 5 ..

I want to change the above dynamic pages to something nice like www.yourdomian.com/5 for page.php and www.yourdomain.com/blog/5 for blog, but that's not reading it from my ReWrite rule in htacces

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?p=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?p=$1

      

Please point me in the right direction on what I am doing wrong. and how to fix it.

+3


source to share


1 answer


You can use these rules in /MyApp/.htaccess

:



Options -MultiViews
RewriteEngine On
RewriteBase /MyApp/

RewriteCond %{THE_REQUEST} /page\.php\?([a-z])=([^&\s]+)
RewriteRule ^ %1/%2? [R=302,NE,L]

RewriteCond %{THE_REQUEST}  /blog\.php\?post=([^&\s]+) [NC]
RewriteRule ^ blog/%1? [R=302,NE,L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^blog/([^/]+)/?$ blog.php?post=$1 [L,NC,QSA]

RewriteRule ^([a-z])/([^/.]+)/?$ page.php?$1=$2 [L,QSA]

      

+1


source







All Articles