URL rewrite to remove parameters

青春壹個敷衍的年華 提交于 2019-12-31 06:42:26

问题



I'm working on a site where all the pages are actually index.php + a 'name' parameter that is analyzed and loads the appropriate template and content.
the homepage url is:
http://www.some_site.com/?page=homepage

1. i was asked to "change" the homepage url to:
http://www.some_site.com
can i use url rewite and htaccess for that and if so, what should i write there?

working on my local machine, i tried this code (mode rewrite is enabled):

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule /index.php /index.php?page=homepage
</IfModule>

i would still need the 'name' parameter to be available to the php code of course, so i can load the template and css files.

2. it would be nice for other pages (not homepage) to be converted from (example)
http://www.some_site.com/?page=products
to:
http://www.some_site.com/products
this is less crucial.

thanx in advance and have a nice day :-)


回答1:


You don't need a rewrite rule at all. Just change your index.php file to show the homepage when there is no page variable at all.

if (!isset($_GET['page'])) {
    $_GET['page'] = 'homepage';
}

For educational purposes, the rewrite rule:

RewriteRule /$ index.php?page=homepage [L]

That is, the URI to match is just the slash (the URI starts after your domain in the URL). The $ means that there should be no characters after the slash.

As for products and such, assuming single words made of only letters:

RewriteRule /([a-zA-Z]+)$ index.php?page=$1 [L]



回答2:


The following should be what you're looking for (for your second, less crucial, question). Put it in your .htaccess-file:

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


来源:https://stackoverflow.com/questions/4910344/url-rewrite-to-remove-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!