为什么我不能将index.php重定向到域?

时间:2015-06-08 09:34:43

标签: php .htaccess

新的.htaccess文件(仍然无效,它会将我发送到localhost/xampp

RewriteEngine on
RewriteBase /sample/

# set root to index.php
DirectoryIndex index.php 

# prevent directory listing
Options -Indexes

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC] 

# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]

下面的旧.htaccess文件:

RewriteEngine on

# set root to index.php
DirectoryIndex index.php 

# prevent directory listing
Options -Indexes

# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# remove index.php
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://localhost/sample/ [R=301,L]

域名为http://localhost/sample/。我尝试了很多谷歌给我的东西,但没有一件能起作用。每次我转到localhost/sample/index.php时,它都不会重定向到localhost/sample/。我还使用了Redirect 301 /index.php /

我试过

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

但它将我发送给http://localhost/xampp/splash.php

我仍然是.htaccess的新手,所以我真的不知道我做错了什么。我已经尝试过对OP说答案有用的问题的答案。

2 个答案:

答案 0 :(得分:0)

您可以使用:

# set root to index.php
DirectoryIndex index.php 
# prevent directory listing
Options -Indexes
RewriteEngine on
RewriteBase /sample/

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC] 

# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
需要

RewriteBase /sample/以确保从当前目录的raltive路径发生重定向。

答案 1 :(得分:0)

使用此:

RewriteEngine On

# prevent directory listing
Options -Indexes

# allows leaving off the extension (.php) in urls
Options +MultiViews

# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^index(\.php)?$ http://localhost/sample [R=301,L]