自定义 .htaccess 重写规则

发布于 2024-12-25 09:32:35 字数 619 浏览 1 评论 0原文

我想将所有内容重定向到一个脚本,例如 index.php?url=inputurl

使用 if/else 我想解析

index.php 中的 url,在我的自定义表中运行查询 url

  • if url is mach: echo "ok"
  • else 不执行任何操作

我应该如何在 WordPress 的根文件夹中设置 .htaccess?

示例:

custom_table 中的 URL:

  • asd
  • dfg
  • ghj

如果用户放置:

www.mysite.com/asd

-> mod_rewrite 应该输出: www.mysite.com/index.php?url=asd

Else 如果用户输入:

www.mysite.com/zzz

->什么也不做

I want to redirect all to one script e.g. index.php?url=inputurl

With if/else I want to parse url

in index.php run query for url in my custom table

  • if url is mach: echo "ok"
  • else do nothing

How should I set .htaccess in root folder of Wordpress?

Example:

URLs in custom_table:

  • asd
  • dfg
  • ghj

If user puts:

www.mysite.com/asd

-> mod_rewrite should output this: www.mysite.com/index.php?url=asd

Else if user puts:

www.mysite.com/zzz

-> do nothing

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

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

发布评论

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

评论(2

与风相奔跑 2025-01-01 09:32:35

我认为以下 .htaccess 应该可以解决问题:

RewriteEngine On

# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]

编辑: 在重写中不包含文件夹和文件(如 /js、/css 等),请在 之前添加以下行RewriteRule 行(参见注释):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

在 PHP 脚本中:

$url = $_GET['url'];

// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
    // do something here
    // maybe redirect with header('Location: path')
} else {
    // show a not found page (error 404)
}

I think the following .htaccess should solve the problem:

RewriteEngine On

# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]

Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

And in the PHP script:

$url = $_GET['url'];

// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
    // do something here
    // maybe redirect with header('Location: path')
} else {
    // show a not found page (error 404)
}
无人问我粥可暖 2025-01-01 09:32:35

您希望能够从数据库中读取数据,并且如果不匹配则不执行任何操作。

这将需要您运行代码来访问数据库,然后返回到 apache 进行处理,并且从 .htacccess 是不可能的(尽管它是来自 httpd.conf)。

.htaccess 解决方案是指定内联的所有“表”条目,如下所示。

RewriteEngine on
RewriteBase /

#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]

You want to both be able to read from a database and do nothing if there is not match.

This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).

The .htaccess solution would be to specify all the "table" entries inline as below.

RewriteEngine on
RewriteBase /

#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文