2

当前网址:通过htaccess的删除ID和index.php文件在子目录

domain/index.php?id=p123 

我想:

  1. 加上www
  2. 删除:index.php文件
  3. 删除'id'表格网址

我做这种方式:

RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC] 
RewriteRule^/%1? [R=302,L,NE] 

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

RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC] 
RewriteRule^/%1? [R=302,L,NE] 


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

RewriteCond %{HTTP_HOST} !^$ 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

它的工作原理,如果DOMIAN是没有子目录。

我怎样才能改变这种代码支持子目录太像这样:

domian/subdirectory/index.php?id=p123 

回答

3

我会去这样理解:

首先,重定向非www的网址WWW的:

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

重写友好的URL,以各自的index.php文件S:

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

接下来,处理与index.php原始的URI在其中:

RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC] 
RewriteRule^/%1%2? [R=301,L] 

最后一个规则也将照顾网址像domain.com/dir/?id=sthdomain.com/dir/index.php?id=sth

2

您可以用做到这一点以下变化:

RewriteCond %{THE_REQUEST} \ /(.*/|)(?:index\.php)?\?id=([^\s&]+) [NC] 
RewriteRule^/%1%2? [R=302,L,NE] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !index\.php 
RewriteRule ^(.*/|)([^/]*)$ $1index.php?id=$2 [L] 

我们知道w %{THE_REQUEST}的格式为GET /path/to/index.php?id=12312412 HTTP/1.1或沿着这些行的内容。在第一条规则中,我们匹配\,它是一个文字空间,然后是始终存在的开始斜线。 (.*/|)将匹配所有东西,直到index.php,或者完全没有。我们在第二条规则中也采用了相同的技巧。我添加了RewriteCond %{REQUEST_URI} !index\.php以防止在特定目录中不存在index.php时出现无限循环。

+0

heh,与我的^ _ ^很相似的答案。虽然我用一分钟打败了你;-) – hjpotter92

+0

感谢您的注意。 – partiz

+1

@ hjpotter92但我的答案包含两个额外的'|',你的答案不。所以:哈哈,开玩笑吧,我赢了! ;-) – Sumurai8

相关问题