简体   繁体   English

Apache 2.2 mod_rewrite 帮助

[英]Apache 2.2 mod_rewrite help

I am trying to get Apache 2.2 mod_rewrite to get clean urls.我正在尝试让 Apache 2.2 mod_rewrite 获得干净的网址。 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和 url 出来是

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

I would like to clean the url in the address bar it to this我想把地址栏中的 url 清理到这个

http://example.com/user/pageName

Edit: This is what's in my httpd.conf if it's any use.编辑:如果有任何用处,这就是我的 httpd.conf 中的内容。

<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使用 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] 

Edit: the rest of my .htaccess编辑:我的 .htaccess 的 rest

#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.在 html 结构中相同的所有菜单链接上不工作相同,并且不能正常工作。 IE: It sends me to http://example.com/user/?view=pageName , but is loading the root HTML file content. IE:它将我发送到http://example.com/user/?view=pageName ,但正在加载根 HTML 文件内容。

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.编辑: access.log 和 error.log 没有错误。

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你错过了 + 所以你不匹配单词,只是一个字符

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

try this..(one slash removed)试试这个..(删除一个斜线)

Fixed: Had to change HTML URL style to be used as such已修复:必须更改 HTML URL 样式才能使用

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

Then in .htaccess然后在.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.还必须从 apache 的 httpd.conf 中禁用 mod_cache.so,因为如果留下任何缓存,它会干扰重写。 Hope this helps others to get clean urls working.希望这可以帮助其他人获得干净的网址。

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

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