繁体   English   中英

如何使用htaccess代码在PHP中重写URL?

[英]How to use htaccess code for URL rewrite in PHP?

我有一个如下链接:

/country/search.php

<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">

当我使用下面的.htaccess代码时,它什么都不做:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L] 

它在工具中具有以下功能:

http://example.com/0/0/4/122

...但是当我单击该链接时,它将再次显示旧的URL。

这里有什么问题?

我正在使用此工具生成该.htaccess代码: http : //www.generateit.net/mod-rewrite/

用以下代码替换代码:

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

# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]

# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]

我认为您误解了重写规则在这种情况下想要实现的目标。

铁这个改写规则

RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2

将确保将对http://example.com/mymethod/myarg的请求重写为

http://example.com/app.php?method=mymethod&arg=myarg

从应用程序内部生成链接时,您应该知道如何构造重写规则,并且必须相应地构建链接。

原始网址:

href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>

理想网址:

/India/andhra%20Pradesh

和.htaccess代码:

RewriteEngine On 

RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

暂无
暂无

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

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