[英]Using .htaccess to rewrite a simple url
I have a url like this - www.example.com/blog.php/username, it will take you to the username's blog page, but I want to use a url like this instead - www.example.com/blog/username to get to the same file (blog.php). 我有一个这样的网址 - www.example.com/blog.php/username,它会带你到用户名的博客页面,但我想用这样的网址 - www.example.com/blog/username来到同一个文件(blog.php)。 Please what are the steps I need to take to achieve this?
请为实现这一目标需要采取哪些步骤? Thanks for any help.
谢谢你的帮助。
Create a file in your root direstory named .htaccess and past this in : 在名为.htaccess的根目录中创建一个文件,然后在以下文件中输入:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/(.*)$ yourpage.php?username=$1 [L]
</IfModule>
(.*) will take any caracters If you want to use only letters and numbers change (.*)
by ([A-Za-z0-9])
(。*)将采用任何字符如果你只想使用字母和数字改变
(.*)
by ([A-Za-z0-9])
Enable mod_rewrite and .htacess. 启用mod_rewrite和.htacess。 Then add this code in your .htaccess under DOCUMENT_ROOT:
然后在DOCUMENT_ROOT下的.htaccess中添加此代码:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# finally this is your rewrite rule
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]
Removing file extension: 删除文件扩展名:
There are two ways to do this.
有两种方法可以做到这一点。 The easy way which just looks at a request, if the requested filename doesn't exist, then it looks for the filename with a .php (or .asp or whatever) extension.
只查看请求的简单方法,如果请求的文件名不存在,那么它会查找带有.php(或.asp或其他)扩展名的文件名。 In an .htaccess file:
在.htaccess文件中:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
Now if you go to: http://domain/about the server will interpret it as if you went to http://domain/about.php .
现在,如果你去: http:// domain / about服务器将解释它,就好像你去了http://domain/about.php 。
Makes sense, but if we're already breaking the relation between URL and filename, we may as well break it intelligently.
有道理,但如果我们已经打破了URL和文件名之间的关系,我们也可以智能地打破它。 Change that .htaccess file:
更改.htaccess文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Source: Dave Dash -> http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/ 资料来源:Dave Dash - > http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/
1 You should install mod-rewrite 1你应该安装mod-rewrite
2 You should make sure that for your doc root directory is "AllowOverride All". 2您应该确保您的doc根目录是“AllowOverride All”。 Ordinary after virtualhost configuration you should find something like:
普通的虚拟主机配置后你应该找到类似的东西:
<Directory "/your/doc/root/path/">
AllowOverride All
</Directory>
3 After that you should start rewrite engine and create rewrite rule in your .htaccess as David Bélanger advises: 3之后你应该开始重写引擎并在你的.htaccess中创建重写规则,正如DavidBélanger建议的那样:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/(.*)$ blog.php/$1 [L]
</IfModule>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.