2014-10-26 145 views
0

我想表明我的网页网址:如何友好的URL转换的搜索引擎优化PHP页面

www.mypage.com/?q=picture 

相反的:

www.mypage.com/picture.html 

我不知道如何将网页重定向我想看看。但是URL不能显示网页扩展 我已经试过这样的事情,但它不工作:

<a href="?q=home" style="text-decoration: none; color: #1E90FF; font-size:25px;">Home</a> 
<?php 
if(isset($_GET['q']) && $_GET['q']!="") 
    { 
     $ext=".php";  
     $page=$_GET['q'].$ext; 
     if(file_exists($page)) 
     { 
      header('location:$page'); 
     } 
     else 
     { 
      header('location:errorpage.php'); 
     } 
    }  
?> 
+0

您的代码无关用'.htaccess'我想你要找的'mod_rewrite的'http://httpd.apache.org/docs/current/mod/mod_rewrite.html – sharf 2014-10-26 21:46:26

+0

我不寻找任何mod_rewrite ..我只想使用我的代码,但是当我尝试我的代码,它不是重定向页面我想要。就是这样 – 2014-10-27 03:34:22

+0

尝试更改$ ext为“.html” – sharf 2014-10-27 23:22:21

回答

0

使用的.htaccess。创建一个名为.htaccess的文件并添加下面的代码。希望这个作品

#rewrite file 
RewriteEngine on 

RewriteRule ^?q=([0-9a-zA-Z]+) $1.html 

唯一的问题是,我可以去www.mypage.com/?q=mario,它会寻找mario.html。

如果这不起作用,然后使用此。

#rewrite file 
RewriteEngine on 

RewriteRule ^home home.html 
RewriteRule ^picture picture.html 

这将使www.mysite.com/picture显示的所有内容,就像观看picture.html

+0

我不想使用.htaccess。无论如何谢谢,寻找更多的答案 – 2014-10-27 03:32:43

相关问题