简体   繁体   中英

Can't Change PHP Switch Case URL

This has been bothering me for awhile. I have some pages on my website that use PHP switch cases to change the content of the page. The problem I'm having is that the URLs for these pages are not very SEO friendly.

I want to change this:

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

to this:

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

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

I've tried .htaccess and nothing works. Here is what I've tried in .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]

Other commands in .htaccess work so I know it's not the file or the webserver.

This is the switch statement in 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;
}
?>

Also, this is the script behind the menus that link the pages.

    <?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>'; } ?>
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]

One possibility you could try is sending all information to a core file such as index.php and then serving the correct data from their

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

Parse the URL in index.php and then either bring in the data via include statements and populate the $_GET array or call the templates and populate the variables from the database.

This would be the way I would do it - probably after spending weeks of trying to get the regular expression correct. Mainly because regular expressions and htaccess rarely do what I want them to do (my own limitation rather than that of regex).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM