.htaccess 如何使用htaccess重写规则处理内部不同的外部url?

.htaccess 如何使用htaccess重写规则处理内部不同的外部url?,.htaccess,url-rewriting,.htaccess,Url Rewriting,新编辑:在我的wordpress localhost站点中,使用.htaccess,我试图告诉服务器处理格式为https://example.com/store/foobar123ashttps://example.com/store/product.php?p=foobar123[这样,服务器就能够通过抓取参数foobar来处理product.php的php代码输出。注意,访问者会继续看到https://example.com/store/foobar123在他的地址栏中,但服务器应在内部正确

新编辑:在我的wordpress localhost站点中,使用.htaccess,我试图告诉服务器处理格式为
https://example.com/store/foobar123
as
https://example.com/store/product.php?p=foobar123
[这样,服务器就能够通过抓取参数
foobar
来处理
product.php
的php代码输出。注意,访问者会继续看到
https://example.com/store/foobar123
在他的地址栏中,但服务器应在内部正确地将此url处理为
https://example.com/store/product.php?p=foobar123

RewriteEngine on
RewriteRule "^/store/(.*)$"  "/store/product.php?p=$1" [PT]
我在htaccess方面是一个绝对的初学者。我尝试了下面的代码和我有限的理解力下的许多其他代码,但它不起作用/给出了预期的结果:

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^store/(.*)$ /store/product.php?p=$1 [L,QSA,NC]
htaccess代码应该是什么,以便访问者在打开链接时
https://example.com/store/foobar123
在地址栏中将继续看到
https://example.com/store/foobar123
但服务器会在内部正确地将此请求解释为
https://example.com/store/product.php?p=foobar123
和di通过抓取参数
p
的值
foobar
,显示文件
product.php
中代码的php输出,并在页面上向访问者显示结果

*新建更多编辑1*:我还尝试了以下代码

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/(.*)$ store/product.php?p=$1 [QSA,L]

如果我访问
https://example.com/store/foobar123
出现错误403禁止访问/未找到页面

但是如果我访问
https://example.com/store/product.php?p=foobar123
然后此链接正常打开,因此我认为这意味着我在上面的htaccess代码中犯了一些错误。如何修复它

*新建更多编辑2*:我还使用本教程尝试了以下代码[。如果我打开
https://example.com/store/foobar123
。请注意,如果直接打开
https://example.com/store/product.php?p=foobar123

RewriteEngine on
RewriteRule "^/store/(.*)$"  "/store/product.php?p=$1" [PT]

如果原始URL格式为
https://example.com/store/product.php?p=foobar123
您想将其更改为
https://example.com/store/foobar123
那么这是您可以在
public\u html/.htaccess

RewriteEngine on
RewriteCond %{THE_REQUEST} /store/product\.php\?p=([^\s]+) [NC]
RewriteRule .+ /store/%1? [L,R]
RewriteRule ^store/((?!product\.php).+)/?$ /store/product.php?p=$1 [L,NC]

这会将您的新URL映射到旧的原始URL。

它不起作用,URL仍显示为
https://example.com/store/product.php?p=foobar123
谢谢,但还是一样,显示为
https://example.com/store/product.php?p=foobar123
idk,如果这有帮助,但我在本地主机上wordpress@simon放这段代码在WordPress规则之前放在htaccess的顶部,否则这些规则会覆盖它。@simon很抱歉回复太晚。事实上,如果您键入原始URL,然后将其转换为新的短格式,这应该会起作用。例如,
/store/product.php?p=foobar
将更改为
/store/foobar
和sh您现在可以查看原始文件的输出。请您进一步说明此规则存在的问题。当您在浏览器中键入URL时,会发生什么情况?我已根据您的回答正确更改了问题,请您再次检查。将代码放入.htaccess后,如果我将鼠标悬停在超链接上,我将t显示为
https://example.com/store/product.php?p=foobar123
但如果我在浏览器中打开此链接,它将作为
https://example.com/store/foobar123
&找不到错误对象。我正在
localhost/testsite
上测试它,所以原始链接看起来像
http://localhost/testsite/store/product.php?p=foobar123
而新链接看起来是
http://localhost/testsite/store/foobar123
但它给了我一个错误“找不到对象”,当访问者将鼠标悬停在链接上时,它应该看起来是
https://example.com/store/foobar123
”-然后您必须确保系统输出的HTML代码包含该版本。这部分问题不是mod_rewrite的责任。@04FS,我正在添加
https://example.com/store/product.php?p=
usignphp在
foo
前面。如果你要编辑我的wordpress帖子,你会发现已经解密他们的
foo
版本。我在其他网站看到过,我只是不知道他们是如何隐藏
product.php?p=
。另外
foo
是其他URL的加密版本。我不明白你在这里的意思,但也没什么关系。如果你想在状态栏中显示
https://example.com/store/foobar123
当我将鼠标悬停在一个链接上时,那一定是该链接的
href
属性包含的URL。@04FS所以你的意思是我必须使用加密来隐藏
product.php?p=
?正如你所说,mod_rewrite无法满足我的需要。我只想从访问者那里隐藏/模糊
product.php?p=
,你必须做任何事情创建您的HTML,输出您希望网站访问者首先看到的正确URL。您的HTML需要链接到
https://example.com/store/foobar123
-这就是用户将看到的内容,以及将向服务器发送的请求。在服务器端,这将被重写为
/store/product.php?p=…
interna这是一个两步的过程,有两个地方你需要改变。