如何用 htaccess 做到这一点?

发布于 2025-01-05 07:22:14 字数 734 浏览 0 评论 0原文

如何使用 htacess 做到这一点:

http://example.com/category => /index.php?action=category
http://example.com/category?query=string => /index.php?action=category&query=string
http://example.com/category/subcategory => /index.php?action=category/subcategory
http://subdomain.example.com => /index.php?action=subdomain
http://subdomain.example.com/category/subcategory => /index.php?action=subdomain/category/subcategory

这是我当前的代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)?$ /index.php?action=$1 [L]

How to make this with htacess:

http://example.com/category => /index.php?action=category
http://example.com/category?query=string => /index.php?action=category&query=string
http://example.com/category/subcategory => /index.php?action=category/subcategory
http://subdomain.example.com => /index.php?action=subdomain
http://subdomain.example.com/category/subcategory => /index.php?action=subdomain/category/subcategory

This is My current code:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)?$ /index.php?action=$1 [L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2025-01-12 07:22:14

以下重写规则将重写:

http://subdomain.domain.com => /index.php?action=subdomain/

我建议您在 index.php 中处理它,以避免额外的重写规则。

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(subdomain)\. [NC]

RewriteRule ^(.*)$ /index.php?action=%1/$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(domain)\. [NC]

RewriteRule ^(category)(^/.*)?$ /index.php?action=$1$2 [L,QSA]

The below rewrite rules will rewrite:

http://subdomain.domain.com => /index.php?action=subdomain/

I suggest you to handle that in your index.php to avoid extra rewrite rules.

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(subdomain)\. [NC]

RewriteRule ^(.*)$ /index.php?action=%1/$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(domain)\. [NC]

RewriteRule ^(category)(^/.*)?$ /index.php?action=$1$2 [L,QSA]
奶茶白久 2025-01-12 07:22:14

谢谢你的帮助。

.htaccess 代码:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

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

index.php 代码:

$domain = "domain.com";

if($_SERVER["HTTP_HOST"] != $domain)
{
    $subdomain = str_replace(".$domain","",$_SERVER["HTTP_HOST"]);
    $_GET['action'] = (isset($_GET['action'])) ? "$subdomain/".$_GET['action']:$subdomain;
}

Thanks for helping.

.htaccess code:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

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

index.php code:

$domain = "domain.com";

if($_SERVER["HTTP_HOST"] != $domain)
{
    $subdomain = str_replace(".$domain","",$_SERVER["HTTP_HOST"]);
    $_GET['action'] = (isset($_GET['action'])) ? "$subdomain/".$_GET['action']:$subdomain;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文