无法使用URL重写向URL添加关键字

时间:2016-03-30 10:36:52

标签: php .htaccess url-rewriting url-routing url-rewrite-module

我正根据以下建议更新我的问题

我的.htaccess重写文件代码 -

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L]

</IfModule>

但是上面.htaccess规则并没有改变我的网址。如果您转到下面的网址,则不会按规则重写网址

网址 - http://expertsusa.org/SearchResult.php?category_slug=t

Url应该重写为 http://expertsusa.org/SearchResult/t

3 个答案:

答案 0 :(得分:0)

订单很重要

<IfModule mod_rewrite.c>
RewriteEngine On

# browser requests PHP

RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

#It is to check whether Request if for PHP File
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

</IfModule>

答案 1 :(得分:0)

请尝试以下规则:

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /SearchResult\.php\?category_slug=([^\s&]+) [NC]
RewriteRule ^ /SearchResult/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

答案 2 :(得分:0)

首先在.htaccess文件中尝试下面的代码 -

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule    ^/SearchResult/$1   SearchResult.php?id=$1    [NC,L]    
</IfModule>

以上代码适用于SearchResult.php页面为了添加更多页面,您可以使用以下类似的代码,例如About.php,您可以在下面使用 -

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule    ^/SearchResult/$1   SearchResult.php?id=$1    [NC,L]    

RewriteRule    ^/About/$1   About.php    [NC,L]    

</IfModule>

在您的情况下,您的网址将在您更改.htaccess文件之后按需要运行。但是你的页面css在此之后被丢失,因为你js并且特别是你的css文件没有根路径尝试下面的解决方案 -

转换以下代码行

 <link href="css/bootstrap.min.css" rel="stylesheet" async>
    <script src="js/jquery.min.js" ></script>
    <script src="js/bootstrap.min.js" ></script>
    <script src="js/alertify.min.js" type="text/javascript" ></script>

到下面

<link href="/css/bootstrap.min.css" rel="stylesheet" async>
<script src="/js/jquery.min.js" ></script>
<script src="/js/bootstrap.min.js" ></script>
<script src="/js/alertify.min.js" type="text/javascript" ></script>