mod_rewrite的力量WWW preFIX选择工作力量、工作、mod_rewrite、preFIX

2023-09-02 20:34:30 作者:挽手说余生

我利用其他mod_rewrite的情况在我的.htaccess文件,并希望确保我的域名总是$ pfixed与WWW(会话存储和搜索引擎优化)p $。但是我目前的.htaccess如果我去到example.com,它的伟大工程,但如果我去example.com/folder~~V它会重定向到www.example.com(离开了/文件夹中)有什么我做错了在这里?

我的.htaccess文件

 选项+了FollowSymLinks
RewriteEngine叙述上

的RewriteCond%{HTTP_HOST} ^例如 .COM $
重写规则^(。*)$ http://www.example.com/$1 [R = 301,L]

#规则:动作
重写规则^([-_!〜*'()$ A-ZA-Z0-9] +)/ $ ./date/index.php?action=$1 [L,QSA]
 

解决方案

重写引擎是多通。它循环采摘最低的.htaccess 的路径与 RewriteEngine叙述在上的文件,直至不再重写发生变化。因此,对于这个 example.com/folder/xxx 请求 - 如果的.htaccess 文件存在于 DOCROOT /文件夹那么它将使用和 DOCROOT /的.htaccess 将 忽略

您可以在DOCROOT或层次使用单个的.htaccess 。单DOCROOT 的.htaccess 文件中有一个小的性能优势,因为发动机只需要打开和读取单个的.htaccess 文件。我描述得更详细些这篇文章,中更多使用.htaccess文件重写规则

辐射4 重制辐射3 mod时隔近一年开发工作恢复

您没有指定一个的RewriteBase 键,以便引擎可以常常失去目录,在这里你的情况。所以,我会用在以下 DOCROOT /的.htaccess

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{HTTP_HOST} = example.com
重写规则^ * http://www.example.com/$0 [R = 301,L]
 

在这里DOCROOT是Apache的文档根目录。

我也很好奇你的包容性格的日期格式设置。似乎很奇怪,我,但离开这个放在一边,最好写成

 重写规则^([-_!〜*'()$  W] +)/ $日期/的index.php?行动= $ 1 [L,QSA]
 

假设,当然,要激活DOCROOT /日期/ index.php文件

I am utilizing other mod_rewrite conditions in my .htaccess file and also want to ensure that my domain is always prefixed with www (for session storing and seo). However with my current .htaccess if I go to example.com it works great, but if I go to example.com/folder it will redirect to www.example.com (leaving out /folder) Is there something I'm doing wrong here?

my .htaccess file

Options +FollowSymLinks  
RewriteEngine on  

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

# Rules for: actions
RewriteRule ^([-_!~*'()$a-zA-Z0-9]+)/?$ ./date/index.php?action=$1 [L,QSA]

解决方案

The rewrite engine is multi-pass. It loops picking the lowest .htaccess file on the path with RewriteEngine On until no further rewrite changes occur. So for this example.com/folder/xxx request -- if an .htaccess file exists in DOCROOT/folder then it will be used and the rewrite rules in DOCROOT/.htaccess will be ignored.

You can use a single .htaccess in DOCROOT or a hierarchy. The single DOCROOT .htaccess file has a small performance advantage as the engine only needs to open and to read in a single .htaccess file. I describe this in more detail in this article, More on using Rewrite rules in .htaccess files

You are not specifying a RewriteBase and so the engine can often lose directories as here in your case. So I would use the following in DOCROOT/.htaccess:

Options +FollowSymLinks  
RewriteEngine on  
RewriteBase   /
RewriteCond   %{HTTP_HOST}        =example.com
RewriteRule   ^.*                 http://www.example.com/$0   [R=301,L] 

where DOCROOT is your Apache document root directory.

I am also curious about your inclusion character set for the date pattern. Seems odd to me, but leaving this aside, it's better written as

RewriteRule ^([-_!~*'()$w]+)/?$  date/index.php?action=$1    [L,QSA]

assuming, of course, that you want to activate DOCROOT/date/index.php