简体   繁体   English

如何在 GCP 上使用 .htaccess 进行文件重写。php?

[英]How to use .htaccess for file rewriting .php on GCP?

I have a .htaccess file which is rewriting all of my urls from path/to/file.php to just path/to/file .我有一个 .htaccess 文件,它正在将我的所有 url 从path/to/file.phppath/to/file

I had this working locally on WAMP, and it also worked on a domain hosted by blacknight.我在 WAMP 上本地工作,它也在 blacknight 托管的域上工作。

However, we are moving to Google Cloud Platform using Debian 9, and it does not work here at all.但是,我们正在使用 Debian 9 迁移到谷歌云平台,它在这里根本不起作用。 If I do not include the.php extension I will get a 404 error.如果我不包含 .php 扩展名,我会收到 404 错误。

Below is my full .htaccess file:以下是我的完整 .htaccess 文件:

Options -MultiViews
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^post/([^\/]+)/?$ post.php?url=$1 [L,NC,QSA]
RewriteRule ^payment/([^\/]+)/?$ payment.php?sub=$1 [L,NC,QSA]
RewriteRule ^player/([^\/]+)/?$ player.php?id=$1 [L,NC,QSA]

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 week"
    ExpiresByType image/jpeg "access 1 week"
    ExpiresByType image/gif "access 1 week"
    ExpiresByType image/png "access 1 week"
    ExpiresByType text/css "access 1 hour"
    ExpiresByType text/x-javascript "access 1 hour"
</IfModule>

<IfModule mod_headers.c>
    Header set Connection keep-alive
    <filesmatch "\.(ico|gif|jpg|jpeg|png)$">
        Header set Cache-Control "max-age=604800, public"
    </filesmatch>
    <filesmatch "\.(css)$">
        Header set Cache-Control "max-age=3600, private"
    </filesmatch>
    <filesmatch "\.(js)$">
        Header set Cache-Control "max-age=3600, private"
    </filesmatch>
    <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "max-age=0, private, must-revalidate"
    </filesMatch>
</IfModule>

What can I do to resolve this or is there another way of rewriting URLs in Google Cloud Platform?我可以做些什么来解决这个问题,或者是否有另一种方法可以在 Google Cloud Platform 中重写 URL?

If your Apache server is enabled you should check if the AllowOverride directive is enabled as well (set to "All" or allowing specific options).如果您的 Apache 服务器已启用,您应该检查 AllowOverride 指令是否也已启用(设置为“全部”或允许特定选项)。 Otherwise Apache will not consider the directives that you put there.否则 Apache 不会考虑你放在那里的指令。

This should usually be placed in your Virtual server config file or the main one.这通常应该放在您的虚拟服务器配置文件或主配置文件中。

Here is an example:这是一个例子:

<Directory "/path/from/where/you/serve/files">
    AllowOverride All
</Directory>

More info: https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride更多信息: https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride

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

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