.htaccess Htaccess URL重写问题

.htaccess Htaccess URL重写问题,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我在使用Apache时遇到了一个挑战 在.htaccess文件中,我希望转换如下请求: url/portfolio/filename.htm 致: url?文件名 有人要吗?感谢您抽出时间尽管我还没有测试过,但类似的功能应该可以使用 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite a

我在使用Apache时遇到了一个挑战

在.htaccess文件中,我希望转换如下请求:

url/portfolio/filename.htm

致:

url?文件名


有人要吗?感谢您抽出时间

尽管我还没有测试过,但类似的功能应该可以使用

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]


</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
#将所有其他URL重写为index.php/URL
重写规则^(.*)/portfolio/(.*)\.htm$$1?$2[PT,L]
ErrorDocument 404 index.php

根据您希望URL对访问者(和搜索引擎)的显示方式,您有两种选择

如果您希望外部可见的URL保持为
URL/portfolio/filename.htm
,则在我删除了两行
RewriteCond
之后,Alec的解决方案仍然有效

RewriteEngine On

RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
如果查询字符串中可能有其他参数,并且您希望保留这些参数,请将
QSA
添加到规则末尾方括号中的选项中

如果希望外部人员看到
url?filename
,请将规则更改为:

RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [L,R]
关于其他查询参数的相同限定也适用

如果这仍然没有帮助,我建议您打开重写日志,并在日志中查找更多线索。把它们贴在这里,有人会帮忙的。您可能必须将此部分放入
httpd.conf
。我的Apache不喜欢
.htaccess
中的它

RewriteLog ...path...
RewriteLogLevel 3  # you'll regret anything higher
这应该起作用:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ index.php?$1 [NC,L]
或者,如果您想明显地更改它:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]
对于用户来说,第二个类似于example.com/?filename,而第一个类似于example.com/portfolio/filename.htm


希望能有所帮助。

Alec,非常感谢您的快速回复。我试过了,但它似乎不起作用,所以我用了一个正则表达式测试仪来看看可能会出现什么情况……是否有可能与此发生“分隔符冲突”?请再具体一点。在您的示例中,“url”是什么意思?“投资组合”是固定的吗?