2012-10-25 65 views
1

这一直困扰着我一段时间。我的网站上有一些使用PHP切换案例来更改页面内容的页面。我遇到的问题是这些网页的网址不是很友善。无法更改PHP开关案例URL

我想改变这个 :

http://www.abcprintingink.com/printing.php?page=print-and-mail

这样:

http://www.abcprintingink.com/printing.php/print-and-mail或更好

http://www.abcprintingink.com/printing/print-and-mail

我试过的.htaccess并没有什么作品。以下是我在.htaccess已经试过:

RewriteEngine On 
#RewriteRule ^web.php/([^-]*)$ /web.php?page=$1 [QSA] 

#RewriteRule ^/web.php/?page\=(.*)$ /web.php?page=$1 [l] 

RewriteRule ^web.php/(.*)$ /web.php?page=$1 [l] 

RewriteRule ^web.php/([a-zA-Z0-9\-/\.]+)/?$ web.php?page=$1 [L] 

在.htaccess工作的其他命令,所以我知道它不是文件或Web服务器。

这是printing.php

<?php 
     /* SEO Switch Statements */ 
     $pagename = "printing"; 
     $page = htmlspecialchars($_GET["page"]); 

     switch ($page) { 
case 'print-and-mail': 
       $page_title = 'page title'; 
       $page_description = 'page desc'; 
       $page_nav_active = 'print-and-mail'; 
       $page_content_copy = 'page text'; 
       $template_slider_image = 'image'; 
       break; 
} 
?> 

switch语句此外,这是链接的网页菜单后面的脚本。你可以尝试

<?php $menu=count($navigation); for ($i=0; $i<$menu; $i++) 
{ echo '<div><li class="navigation"><a href="?page='.$navigation[$i].'">' 
.$replaced = str_replace("-", " ", $navigation[$i]).'</a></li></div>'; } ?> 
+0

你是说这些链接不工作(404大概),或者他们确实工作,PHP的switch语句没有接收GET中的值吗? (所以页面显示错误的内容)? –

+0

哦,不,他们工作正常。它只是像“webpage.php?page = content-page”这样的URL显示,无论在.htaccess文件中有重写规则,我都无法让它们以不同的方式显示。 –

+0

如果有帮助我在导航菜单中有这样的代码<?php $ menu = count($ navigation); for($ i = 0; $ i <$ menu; $ i ++){ \t \t \t \t echo'

'; \t \t \t \t \t}?> –

回答

1
RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.+) - [PT,L] 

## 301 redirect, http://www.abcprintingink.com/printing.php?page=print-and-mail 
## to http://www.abcprintingink.com/printing/print-and-mail 
RewriteCond %{QUERY_STRING} ^page=(.*)$ 
RewriteRule ^printing\.php$ /printing/%1? [R=301,L] 

## makes /printing/print-and-mail actually work 
RewriteRule ^printing/(.*)$ /printing\.php?page=$1 [L,QSA] 
+0

我仍然无法让它工作。我把这个重写规则放进去,甚至当我直接键入url时,它会返回一个404错误。 –

+0

@SpyderTech你的根网站文件夹中是否有一个名为printing.php的文件? –

+0

是的,我这样做,那是保存开关case语句的文件 –

0

一种可能性是将所有信息到一个核心文件,如index.php文件,然后从他们的

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

投放正确的数据解析中的index.php,然后要么URL通过包含语句引入数据并填充$ _GET数组或调用模板并填充数据库中的变量。

这将是我会这样做的方式 - 可能花了数周试图获得正确的表达正确。 主要是因为正则表达式和htaccess很少做我想让他们做的事(我自己的限制,而不是正则表达式)。

+0

.htaccess重写选项的工作,但我没有考虑解析url。谢谢。 –