Apache 2.2 mod_rewrite 帮助

发布于 2024-11-11 15:07:08 字数 1830 浏览 0 评论 0原文

我正在尝试让 Apache 2.2 mod_rewrite 获得干净的 url。 我有诸如

<ul>
<li><a href="index.php?view=pageName">Page Name</a></li>
<li><a href="index.php?view=pageName2">Page Name2</a></li>
<li><a href="index.php?view=pageName3">Page Name3</a></li>
</ul>

url 之

http://example.com/user/index.php?view=pageName

类的链接,我想将地址栏中的 url 清理到此

http://example.com/user/pageName

编辑:这是我的 httpd.conf 中的内容(如果有任何用处)。

<Directory "C:/Apache2.2/htdocs/user">
    Options Indexes FollowSymLinks
    AllowOverride all
    order allow,deny
    Allow from all
</Directory>

使用 phpinfo( )我已经验证 mod_rewrite 已加载,并且我的 .htaccess (用户文件,不是 root .htaccess)中有这个,而不是 apache 上的虚拟用户

RewriteEngine on
RewriteBase /user/ #Edited rewrite base added in, but not helping much.
RewriteOptions Inherit
RewriteRule ^/user/([a-zA-Z])/?$ index.php?view=$1  [NC,L] 

编辑:我的 .htaccess 的其余部分

#Ensure browser reads Header
Header unset ETag
FileETag None
Header unset Last-Modified

#Set caching expires
Header set Expires On
ExpiresDefault "access plus 30 days"

#gzip
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>

在 html 结构相同的所有菜单链接上工作不一样,并且无法正常工作。 IE:它将我发送到 http://example.com/user/?view=pageName,但正在加载根 HTML 文件内容。

如果我将此行更改

RewriteRule ^/user/([a-zA-Z])/?$ index.php?view=$1  [NC,L]

RewriteRule ^index\.php$ http://www.google.com  [NC,L]

我会按预期发送到谷歌。显然我在匹配和替换方面肯定做错了什么,但我做错了什么? 提前致谢。

编辑: access.log 和 error.log 没有错误。

I am trying to get Apache 2.2 mod_rewrite to get clean urls.
I have links such as

<ul>
<li><a href="index.php?view=pageName">Page Name</a></li>
<li><a href="index.php?view=pageName2">Page Name2</a></li>
<li><a href="index.php?view=pageName3">Page Name3</a></li>
</ul>

and url comes out to be

http://example.com/user/index.php?view=pageName

I would like to clean the url in the address bar it to this

http://example.com/user/pageName

Edit: This is what's in my httpd.conf if it's any use.

<Directory "C:/Apache2.2/htdocs/user">
    Options Indexes FollowSymLinks
    AllowOverride all
    order allow,deny
    Allow from all
</Directory>

Using phpinfo() I have verified that mod_rewrite is loaded and I have this in my .htaccess (user file, Not root .htaccess) and not virtual user on apache

RewriteEngine on
RewriteBase /user/ #Edited rewrite base added in, but not helping much.
RewriteOptions Inherit
RewriteRule ^/user/([a-zA-Z])/?$ index.php?view=$1  [NC,L] 

Edit: the rest of my .htaccess

#Ensure browser reads Header
Header unset ETag
FileETag None
Header unset Last-Modified

#Set caching expires
Header set Expires On
ExpiresDefault "access plus 30 days"

#gzip
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>

does not work the same on all menu links which are identical in html structure, and not working properly. IE: It sends me to http://example.com/user/?view=pageName, but is loading the root HTML file content.

If I change this line

RewriteRule ^/user/([a-zA-Z])/?$ index.php?view=$1  [NC,L]

to

RewriteRule ^index\.php$ http://www.google.com  [NC,L]

I get sent to google as expected. So obviously I must be doing something wrong with the matching and replacing, but what am I doing wrong?
Thanks in advance.

Edit:
access.log and error.log are error free.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒尘 2024-11-18 15:07:09
RewriteRule ^/user/([a-zA-Z]+)?$ index.php?view=$1  [NC,L]

你缺少 + 所以你没有匹配单词,只是一个字符

RewriteRule ^/user/([a-zA-Z]+)?$ index.php?view=$1  [NC,L]

you are missing the + so you are not matching words, just a single char

屋顶上的小猫咪 2024-11-18 15:07:09
RewriteRule ^/user/([a-zA-Z])?$ index.php?view=$1  [NC,L] 

试试这个..(删除一个斜线)

RewriteRule ^/user/([a-zA-Z])?$ index.php?view=$1  [NC,L] 

try this..(one slash removed)

醉生梦死 2024-11-18 15:07:09

已修复:必须更改要使用的 HTML URL 样式

<a href="pageName">Page Name</a>

然后在 .htaccess

RewriteEngine On
RewriteOptions Inherit
ReWriteBase /user/
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?view=$1 [QSA,L]

还必须从 apache 的 httpd.conf 禁用 mod_cache.so,因为如果留下任何缓存,它会干扰重写。希望这可以帮助其他人获得干净的网址。

Fixed: Had to change HTML URL style to be used as such

<a href="pageName">Page Name</a>

Then in .htaccess

RewriteEngine On
RewriteOptions Inherit
ReWriteBase /user/
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?view=$1 [QSA,L]

Also had to disable mod_cache.so from apache's httpd.conf as it interfere with rewrite if there are any caches left behind. Hope this helps others to get clean urls working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文