gpt4 book ai didi

.htaccess - 通过子目录中的 htaccess 删除 id 和 index.php

转载 作者:行者123 更新时间:2023-12-03 08:03:10 25 4
gpt4 key购买 nike

当前网址:

domain/index.php?id=p123

我要:

  1. 添加 www
  2. 删除:index.php
  3. 从 URL 中删除“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

最佳答案

我会这样处理:

首先,将非 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 文件中:

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 这样的 URL >

关于.htaccess - 通过子目录中的 htaccess 删除 id 和 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33517582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com