dqyin0101 2015-06-18 08:19
浏览 49
已采纳

重写URL无法在localhost中运行

I tried hiding my .php extensions with .htaccess at first.. It did work fine after including this in the file

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

and also including this in my function.php

//==== 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;  
  }  
} 

Thing is the url for dynamic pages did not change.. I still have page.php?p=5 and blog.php?post=5 not changing to page?p=5 and blog?post=5..

I do want to change the above dynamic pages url to something nice like www.yourdomian.com/5 for page.php and www.yourdomain.com/blog/5 for the blog, but this is 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

Kindly point me in the right direction as to what am doing wrong. and how to fix it.

  • 写回答

1条回答 默认 最新

  • dongnaizao8039 2015-06-19 15:03
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题