Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何通过htaccess重写url_Php_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Php 如何通过htaccess重写url

Php 如何通过htaccess重写url,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,我正试图通过.htaccess重写以下URL: http://website.com/dealer_home.php?id=dealer1 RewriteEngine On RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L] 我的目标是这样,其中dealer1是设置为变量的用户名: http://website.com/dealer1 我已在.htaccess中尝试了此规则: http://website.com/dealer_home.php

我正试图通过.htaccess重写以下URL:

http://website.com/dealer_home.php?id=dealer1
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
我的目标是这样,其中
dealer1
是设置为变量的用户名:

http://website.com/dealer1
我已在.htaccess中尝试了此规则:

http://website.com/dealer_home.php?id=dealer1
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
但是,当我尝试加载任何网站页面时,会收到“内部服务器错误”消息

你能告诉我哪里做错了吗

编辑: 我还尝试了
RewriteRule^(.*)$dealer\u home.php?id=$1[PT]
,但没有成功


谢谢大家!

可能与现有文件/文件夹和根uri冲突。
请尝试此代码

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
如果
something
不是现有的文件或文件夹,则此规则将匹配每个url,如
domain.com/something

因此,如果你有其他规则,那么你应该把它们放在这个规则之上


编辑:避免重复内容并将旧格式重定向到新url格式

RewriteEngine On

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

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

我认为你是在正确的方式,因为现在的网页加载ok。问题是URL没有更改。此规则现在允许您使用
http://website.com/dealer1
但它不会重定向
http://website.com/dealer_home.php?id=dealer1
转换为新的url格式。这是你想要的吗?啊,太蠢了,我忘了更改URL重定向。谢谢,现在一切都很好!很高兴它起作用了。无论如何,如果您想将旧格式重定向到新的url格式,我在回答中添加了一个“编辑”部分