简体   繁体   English

RewriteRule不起作用.htaccess

[英]RewriteRule not working .htaccess

So I have this .htaccess: 所以我有这个。

RewriteEngine On
RewriteRule ^([^/]*)/$ /brand.php?brand=$1 [L]

index.php index.php

<a href="/brand.php?brand=audi">audi</a>

brand.php brand.php

<?php
    if(isset($_GET['brand'])) {
        echo "brand is ".$_GET['brand'];
    } else {
        echo "no brand selected";
    }
?>

according to http://www.generateit.net/mod-rewrite/index.php it should be working. 根据http://www.generateit.net/mod-rewrite/index.php,它应该可以工作。

When you click the link, your url will look like domain/brand.php?brand=audi . 当您单击链接时,您的网址将类似于domain/brand.php?brand=audi But I want it to look like domain/audi/ . 但我希望它看起来像domain/audi/ It works fine, so I thought. 它工作正常,所以我想。 But when you type domain/asdvasdvasdvkasdv/, it will just say: brand is asdvasdvasdvkasdv instead of no brand selected , since there was never a brand posted to brand.php. 但是,当您键入domain / asdvasdvasdvkasdv /时,它只会说: brand是asdvasdvasdvkasdv而不是未选择品牌 ,因为从没有发布到brand.php的品牌。

Apache running on linux with mysql version 10.1.22-MariaDB 在mysql版本为10.1.22-MariaDB的Linux上运行的Apache

Your rewriteRule only maps /bradname/ to the old url /brand.php?brand=brandname . 您的rewriteRule仅将/ bradname /映射到旧网址/brand.php?brand=brandname To redirect your old url to the new one, you need an aditional rewriteRule. 要将旧的URL重定向到新的URL,您需要一个附加的rewriteRule。 Add the following before your existing RewriteRule 在现有的RewriteRule之前添加以下内容

RewriteEngine on


#1)old to new uri handler


RewriteCond %{THE_REQUEST} /brand\.php\?brand=([^\s]+) [NC]
RewriteRule ^ /%1/? [L,R]
#2)Your new to old uri handler

This will externally redirect /brand.php?brand=brandname to /brandname/ . 这会将/brand.php?brand=brandname从外部重定向到/ brandname /

Fixed 固定

There are a couple of ways to fix this problem, but I think this is the best one: 有两种方法可以解决此问题,但是我认为这是最好的方法:

.htaccess .htaccess

RewriteEngine On
RewriteRule ^brand/([^/]*)/$ /brand.php?brand=$1 [L]

html html

<a href="/brand/audi/">audi</a>

You will need to link to a subdirectory /brand/ which represents brand.php. 您将需要链接到代表brand.php的子目录/brand/ In brand.php you will simply check if the value of brand is in the database. 在brand.php中,您只需检查品牌的值是否在数据库中。

You will need to link to /brand/brandname/ in order to get brand.php. 您将需要链接到/ brand / brandname /以获得brand.php。 If you use /brand.php?brand=brandame as a link, the URL will not be rewritten. 如果使用/brand.php?brand=brandame作为链接,则不会重写该URL。

这个怎么样?

RewriteRule path/brand.php?brand=(.*)/ path/$1/

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

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