简体   繁体   English

删除 url .php 扩展名和获取请求的参数?

[英]Remove url .php extension and parameters of get request?

My URL is:我的网址是:

https://example.com/detail.php?id=56&subcat=11

I need like this:我需要这样:

https://example.com/detail/56/11

Using .htaccess file i am not able to remove the parameter names (id, subcat) and question mark(?).使用 .htaccess 文件,我无法删除参数名称(id、subcat)和问号(?)。 I have removed only .PHP extension.我只删除了 .PHP 扩展名。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

RewriteCond %{THE_REQUEST} \s/+products(?:\.php)?\?id=([0-9]+) [NC]
RewriteRule ^ products/%1? [R,L]

RewriteRule ^products/([0-9]+)/?$ products.php?id=$1 [L,QSA]

## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Thank you.谢谢你。

Just place the following into your .htaccess file (and make sure that mod_rewrite is enabled):只需将以下内容放入您的 .htaccess 文件中(并确保启用了 mod_rewrite):

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteRule ^index.php/([^/]+)/?([^/]*) /index.php?id=$1&subcat=$2 [NC]

This is a bit complex problem.这是一个有点复杂的问题。 You won't achive it only by .htaccess.您不会仅通过 .htaccess 来实现它。 You have to parse variables from url to array and make it accessible from php code.您必须将变量从 url 解析为数组,并使其可从 php 代码访问。 In this link in section 'Using php' there is some example how to do this.在“使用 php”部分的此链接中,有一些示例说明如何执行此操作。

Because you don't want to use "normal" link, we have to handle requests for pages and manually parse them.因为您不想使用“普通”链接,所以我们必须处理页面请求并手动解析它们。 First define in you htaccess redirect:首先在您的 htaccess 重定向中定义:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]

Then create index.php which will handle redirects.然后创建将处理重定向的index.php

// your available sites
$sites = [ 'detail' ];

$path = $_GET['path'];
$params = explode( "/", $path );
$site = array_shift($params);

if( in_array( $site, $sites ) ){
    include($site.".php");
}

Now in detail.php you can use $params and you get array with you values.现在在 detail.php 中,您可以使用$params并获得包含值的数组。 So our http://example.com/detail/56/1 goes like http://example.com/index.php?path=detail/56/1 .所以我们的http://example.com/detail/56/1就像http://example.com/index.php?path=detail/56/1 With this aproach we will write our logic in details.php and we can use $params , which equals Array ( [0] => 56 [1] => 1 ) .使用这种方法,我们将在 details.php 中编写我们的逻辑,我们可以使用$params ,它等于Array ( [0] => 56 [1] => 1 )

Your link could be more readable in code if you change it to /detail/id/56/subcat/11 .如果您将其更改为/detail/id/56/subcat/11您的链接在代码中可能更具可读性。 Just add this before check in_array只需在检查 in_array 之前添加它

$x = [];
for( $i=0; $i<count($params); $i+=2 )
    $x[$params[$i]] = $params[$i+1];
$params = $x;

This is only simple example of idea.这只是想法的简单示例。 Better use some framework with that routing, like symphony.最好在路由中使用一些框架,比如 Symphony。

you can use following .htaccess sample file Note : replace filename1 and filename2 with your php file names您可以使用以下 .htaccess 示例文件注意:用您的 php 文件名替换 filename1 和 filename2

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(filename1.*)$ filename1.php?path=$1 [NC,L,QSA]
RewriteRule ^(filename2.*)$ filename2.php?path=$1 [NC,L,QSA]

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

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