简体   繁体   English

.htaccess网址重写不起作用?

[英].htaccess url rewriting not working?

I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. 老实说,我对.htaccess经验很少,因为我一直想尽可能地远离它。 However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting. 但是,我最近想整理一下URL,我发现可以通过.htaccess和重写来实现。

Basically, I want to rewrite a url like: 基本上,我想像这样重写网址:

www.mysite.com/profile.php?id=48194 www.mysite.com/profile.php?id=48194

To something like: 像这样:

www.mysite.com/profile/48194 www.mysite.com/profile/48194

Here's the code I have currently: 这是我目前拥有的代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1

The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1 . 我要使用的行位于最底端, RewriteRule ^profile/(.*)/$ profile.php?id=$1 The rest is used to remove the page extensions from the urls. 其余的用于从网址中删除页面扩展。 I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed. 我将$1更改$1 $2以为它可能与上面的代码冲突,但是什么都没有改变。

I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. 我还删除了除RewriteEngine on之外的所有代码RewriteEngine on并在最后一行认为代码可能有冲突,但是再次没有更改或工作。 The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on. 其余代码确实可以正常工作,即从url中删除扩展名,因此我知道重写的事情已经开始了。

Could someone try to break down and explain what I did wrong and how all this works? 有人可以尝试分解并解释我做错了什么以及所有这些工作原理吗? As well as providing a working example of the thing I'm trying to accomplish? 除了提供我要完成的事情的可行示例之外?

Thanks in advance! 提前致谢!

Change order of your rules and use MultiViews option. 更改规则的顺序并使用MultiViews选项。 Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. Option的MultiViewsApache's content negotiation module ,该Apache's content negotiation modulemod_rewrite之前运行,并使Apache服务器匹配文件扩展名。 So /file can be in URL but it will serve /file.php . 因此/file可以位于URL中,但它将提供/file.php

RewriteEngine on
RewriteBase /

RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

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

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