基本的.htaccess URL重写重写、基本、htaccess、URL

2023-09-02 11:27:44 作者:一朵(独)花、

有几件事我想完成我的网站,我似乎无法.htaccess文件做的URL。

There are a few things I want done to the URLs of my site that I cannot seem the .htaccess file to do.

1删除文件扩展名F.E. example.com/file.php example.com/file

1 remove file extension f.e. example.com/file.php should be example.com/file

2卸下WWW。 F.E. www.example.com example.com (我这部分的工作,但如果我会讨厌它以后我把修复,这不再工作

2 remove the www. f.e. www.example.com should be example.com (I got this part to work, but I would hate it if after I put in fix and this no longer worked

3应该没有人能够看到的index.php在根铁年底 example.com/index.php example.com

3 no one should be able to see index.php at the end of root f.e. example.com/index.php should be example.com

4我的博客页面都应该有不错的网址F.E. example.com/blog.php?article=the-name-of-article example.com/blog/the-name-of-文章

4 my blog page should have nice urls f.e. example.com/blog.php?article=the-name-of-article should be example.com/blog/the-name-of-article

下面是我现在的.htaccess文件

here is my current .htaccess file

Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /

## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php

## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]

## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]

当我尝试去博客/的名称 - - 文章我得到一个内部服务器错误。

when I try to go to blog/the-name-of-article I get a internal server error.

推荐答案

从身体和你的.htaccess的意见看来,我会过去,只要它:P

From the body and comments of your .htaccess it appears that I would have provided it in the past :P

唯一错在你的.htaccess的排序规则。始终让他们从最具体到最普通的。让你的code是这样的:

Only thing wrong in your .htaccess is ordering of rules. Always have them from most specific to most generic. Have your code like this:

Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /

## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^blog/(.*)$ /blog.php?article=$1 [QSA,L]

## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php