将php文件中的url重写为任何虚拟目录

时间:2015-02-09 12:06:34

标签: apache .htaccess mod-rewrite url-rewriting

我正在努力将文件重写为虚拟目录。我已经搜索了很多东西,但我没有尝试过的任何东西,所以我的.htaccess的其余部分可能有问题,或者只是与这些规则相冲突。

我想达到什么目标:

Rewrite https://www.domain.com/index.php -> https://www.domain.com/
Rewrite https://www.domain.com/page.php -> https://www.domain.com/any-pagename/

我目前的htaccess(http-> https和非www到www)和我尝试实现我的第一个目标:

# Deflate Compression by MimeType
<IfModule mod_deflate.c>
 <FilesMatch "\.(js|jpg|jpeg|gif|png|css)$">
  ExpiresActive on
  ExpiresDefault "access plus 1 month"
  SetOutputFilter DEFLATE
 </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Here I want to rewrite my https://www.domain.com/index.php
RewriteRule ^/ index.php [L]
Options -Indexes
</IfModule>

1 个答案:

答案 0 :(得分:0)

你最后的规则似乎有缺陷。用此规则替换它:

# Here I want to rewrite my https://www.domain.com/index.php

# 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
RewriteRule ^ index.php [L]