简体   繁体   English

将所有请求重定向到单个页面

[英]Redirect all requests to single page

I have a food blog website, with many posts stored in a database.我有一个美食博客网站,许多帖子存储在数据库中。 When someone clicks on a link to a post, the website searches the database for the post and presents it in a template.当有人点击帖子的链接时,网站会在数据库中搜索帖子并将其显示在模板中。 I don't store an HTML file for each post.我不会为每个帖子存储一个 HTML 文件。

Right now, I do it by handling a 404 error (which goes off because the requested HTML file doesn't exist) and searching the database from there.现在,我通过处理 404 错误(由于请求的 HTML 文件不存在而关闭)并从那里搜索数据库来做到这一点。 I know this isn't right.我知道这是不对的。

How can I set up my web server (Apache running on a Raspberry Pi), so that all requests go to one page, which will do the searching and send the user to the right page?如何设置我的 Web 服务器(在 Raspberry Pi 上运行的 Apache),以便所有请求都转到一个页面,该页面将进行搜索并将用户发送到正确的页面? And is this the right thing to do?这是正确的做法吗?

You can do that with a file you put in your website root named .htaccess and using the mod_rewrite module.您可以使用放在网站根目录中的名为.htaccess并使用 mod_rewrite 模块来执行此操作。 You'll need to make sure .htaccess and mod_rewrite are enabled in the Apache configuration.您需要确保在 Apache 配置中启用了 .htaccess 和 mod_rewrite。 But once enabled you can do something like this:但是一旦启用,您就可以执行以下操作:

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

That will rewrite any URL that doesn't refer to an actual existing file or directory to your index.php which you can use to handle the request.这会将任何不引用实际现有文件或目录的 URL 重写到您的index.php ,您可以使用它来处理请求。

For example, if someone visits http://yourdomain.com/article , the path will be internally rewritten to index.php?path=article and you can access the path using $_GET['path'] .例如,如果有人访问http://yourdomain.com/article ,路径将在内部重写为index.php?path=article ,您可以使用$_GET['path']访问该路径。

Note: If you want to literally serve everything through that single entry point you could remove the two middle lines and that would send everything through there.注意:如果您想通过单个入口点真正地提供所有内容,您可以删除中间的两条线,然后将所有内容发送到那里。 Including images, scripts, CSS etc, which is generally not what you want.包括图像、脚本、CSS 等,这通常不是您想要的。

You need proper .htaccess rules in your public root directory and you need a apache2 mod_rewrite module enabled.您需要在公共根目录中使用适当的 .htaccess 规则,并且需要启用 apache2 mod_rewrite 模块。

On Ubuntu you can can enable it with the following command:在 Ubuntu 上,您可以使用以下命令启用它:

sudo a2enmod rewrite

your .htaccess file could look like the following:您的 .htaccess 文件可能如下所示:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM