使用.htaccess优化长URL

使用.htaccess优化长URL,.htaccess,.htaccess,我有这些网址 http://www.website.com/?goto=plaforms http://www.website.com/?goto=offers&platform=dates http://www.website.com/?goto=profile&member=1 http://www.website.com/?goto=product&offer=2 我想使用.htaccess将其更改为: http://www.website.com/plaform

我有这些网址

http://www.website.com/?goto=plaforms
http://www.website.com/?goto=offers&platform=dates
http://www.website.com/?goto=profile&member=1
http://www.website.com/?goto=product&offer=2
我想使用.htaccess将其更改为:

http://www.website.com/plaforms
http://www.website.com/offers/dates
http://www.website.com/profile/1
http://www.website.com/product/2
我试过了,但没用

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?goto=$1

老实说,我不知道怎么做

更新: 我有一个index.php,在其中调用所有页面

index.php

$page = $_GET['goto'];
$url  = $page.'.php';

if(file_exists($url)):  
  include($url);
else:
  header('location:'?goto=home');
endif;


第一个参数调用页面,第二个参数调用数据库中的数据。您可以编写如下重写规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#The rules
RewriteRule ^([^\/]+)?$ /?goto=$1 [R=301,L]
RewriteRule ^offers\/dates$ /?goto=offers&platform=dates [R=301,L]
RewriteRule ^profile\/(\d+)$ /?goto=profile&member=$1 [R=301,L]
RewriteRule ^product\/(\d+)$ /?goto=product&offer=$1 [R=301,L]

问题是某些参数值无法从您的URL派生。因此,您必须完整地(如/offers/dates)或部分地(如/profile/x和/product/x)说明它们。唯一真正通用的规则是第一个将anything/xxx重写为goto=xxx的规则。因此,根据URL空间的不同,您可能需要编写很多规则。

您可以编写如下重写规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#The rules
RewriteRule ^([^\/]+)?$ /?goto=$1 [R=301,L]
RewriteRule ^offers\/dates$ /?goto=offers&platform=dates [R=301,L]
RewriteRule ^profile\/(\d+)$ /?goto=profile&member=$1 [R=301,L]
RewriteRule ^product\/(\d+)$ /?goto=product&offer=$1 [R=301,L]

问题是某些参数值无法从您的URL派生。因此,您必须完整地(如/offers/dates)或部分地(如/profile/x和/product/x)说明它们。唯一真正通用的规则是第一个将anything/xxx重写为goto=xxx的规则。因此,根据我们的URL空间,您可能需要编写很多规则。

我相信您的答案很好,因为您在问题中已经展示了您的努力。为了更好地理解问题,请告诉我们使用不同查询字符串值(如
&member
&offer
)的逻辑是什么。问题描述“不工作”不工作。@RavinderSingh13我更新了我的问题。我相信你的答案是好的,因为你在你的问题上表现出了你的努力。为了更好地理解问题,请告诉我们使用不同查询字符串值(如
&member
&offer
)的逻辑是什么。问题描述“不工作”不工作。@RavinderSingh13我更新了我的问题。希望能有助于更好的理解