簡體   English   中英

使用.htaccess從URLS中刪除index.php

[英]Removing index.php from URLS with .htaccess

我有一個index.php控制器,所有不是現有文件的URL都重定向到。 我目前的.htaccess規則如下:

RewriteEngine On
DirectorySlash Off

# Remove Trailing Slashes
RedirectMatch 302 ^(.*)/$ $1

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302]

# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

如果index.php包含在URL中,我想要做的是使用301重定向。 但! 我不能使用RewriteBase或以斜杠開頭的查詢,因為我正在開發的webapp可能在子文件夾中。 所以基本上,我只想重寫當前目錄:

example.com/foo/index.php/bar/whatever應該重定向到example.com/foo/bar/whatever example.com/foo/index/bar/whatever應該重定向到example.com/foo/bar/whatever

localhost / place / foo / index.php / bar /無論應該重定向到localhost / place / foo / bar /無論localhost / place / foo / index / bar /還應該重定向到localhost / place / foo / bar /等等

我怎樣才能用.htaccess來完成這個任務?

更新的代碼:此處的所有內容都有效除外:index.php不會從URL中的任何位置刪除。

這是代碼:

<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

# Reroute to index.php
#RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]

</IfModule>

將mod_rewrite規則與mod_alias規則混合使用並不是一個好主意。 試試這個更新的.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

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

# Reroute to index.php
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]
</IfModule>

確保你的服務器上有mod重寫嘗試

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

你可以像這樣使用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM