简体   繁体   English

获取URL参数并使用htaccess清理URL

[英]Get URL paramters and clean the url using htaccess

Im creating a website that have 2 iframes, the first iframe have a song playing and the second one have the website. 我创建了一个有2个iframe的网站,第一个iframe播放了一首歌,第二个有网站。 Here is my index.php file: 这是我的index.php文件:

<?php
$base_url = 'http://www.mywebsite.com/site/';
?>
<iframe src="<?php echo $base_url; ?>music.php" frameborder="0" width="960px" height="1px"></iframe>

<?php
$page = $_GET['params'];

switch ($page) {
    case 'home':
        $page = 'home.php';
        break;
    case 'contact':
        $page = 'contact.php';
        break;
}
?>

<iframe src="<?php echo $base_url; ?><?php echo $page ?>" frameborder="0" width="100%" height="100%"></iframe>

And this is my .htaccess 这是我的.htaccess

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ site/index.php?params=$1

It's working fine but what i need is: 它工作正常,但我需要的是:

When the user try to access http://www.mywebsite.com/contact I need to get this 'contact' for my switch statement and clean the url to http://www.mywebsite.com 当用户尝试访问http://www.mywebsite.com/contact时,我需要为我的switch语句获取此“联系人”并清除网址http://www.mywebsite.com

Simulation: 模拟:

  1. Request http://www.mywebsite.com/contact 请求http://www.mywebsite.com/contact
  2. Get param 'contact' 得到参与'联系'
  3. Load the contact.php file 加载contact.php文件
  4. Clean the url to http://www.mywebsite.com 将网址清理为http://www.mywebsite.com

Thanks in advance! 提前致谢!

You can do the first 3 steps with 您可以执行前3个步骤

RewriteRule ^contact$ /contact.php?params=contact [L,QSA]

if you add that before the rules that you already have. 如果你在你已经拥有的规则之前添加它。 But you can't "clean" the URL to http://www.mywebsite.com/ , since then it will be indistinguishable from site/index.php?params=home . 但是你不能“清理”到http://www.mywebsite.com/的URL,因为那时它与site/index.php?params=home无法区分。

For any "contact" page, then: 对于任何“联系”页面,则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILEMAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php?params=$1 [L,QSA]

You're also going to want to make sure Multiviews is turned off: 您还需要确保关闭Multiviews

Options Multiviews

You need just one generic rule like this: 你只需要一个这样的通用规则:

# To internally forward /anything to /anything.php?params=anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php?params=$1 [L,QSA]

What do you mean by "clean the URL to http://www.mywebsite.com " ? 你是什​​么意思“清理网址到http://www.mywebsite.com ”?

Because if you want to transmit the contact parameter you can use the QSA flag which appends query string (Query String Append) but if you just want to clean URL you can do some treatments in your contact.php and then redirect to main page with PHP header function. 因为如果要传输联系参数,可以使用附加查询字符串的QSA标志(查询字符串追加),但如果您只想清除URL,可以在contact.php中进行一些处理,然后使用PHP重定向到主页面标题功能。

He answered while I was typing my answer but I was about to give you Jon Lin's code 我在输入答案时回答,但我准备给你Jon Lin的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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