简体   繁体   English

如何在 PHP 中定义 .htaccess 规则

[英]How to define .htaccess rule in PHP

I want to add rules in my .htaccess file to redirect my application.我想在我的 .htaccess 文件中添加规则来重定向我的应用程序。

Let me present the problem by an example :让我通过一个例子来说明这个问题:

If user types in the URL => www.myNewProduct.com如果用户输入 URL => www.myNewProduct.com
then redirect URL is => www.myNewProduct.com/index.php然后重定向 URL 是 => www.myNewProduct.com/index.php

And if the user type => www.myNewProduct.com/customer如果用户输入 => www.myNewProduct.com/customer
Then redirect URL is => www.myNewProduct.com/API/MNP/customerService然后重定向 URL => www.myNewProduct.com/API/MNP/customerService

My .htaccees file is :我的 .htaccees 文件是:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(([a-zA-Z0-9\-=&]*))
RewriteRule ^Login/([a-zA-Z0-9\-_]*)$ Login.php?LicenseType=$1&Refresh=1/$1

RewriteCond %{QUERY_STRING} ^(([/docusign))
RewriteRule ^Login/([a-zA-Z0-9\-_]*)$ Login.php?LicenseType=$1&Refresh=1/$1

How to accomplish the task in question ?如何完成有问题的任务?

You cannot "define" htaccess rewrite rules in PHP, what you could do is to set all urls to be handled by the index.php like您不能在 PHP 中“定义”htaccess 重写规则,您可以做的是将所有 url 设置为由 index.php 处理,例如

RewriteEngine on
RewriteRule (.*) index.php?requested=$1 [L]

In php you could fetch the ful url with $_GET['requested'] , you can then do further processing.在 php 中,您可以使用$_GET['requested']获取完整的 url,然后您可以进行进一步的处理。

You should definitely take a look at htaccess/rewriterules.你绝对应该看看 htaccess/rewriterules。

Create a file named .htaccess on the root directory of your domain, then add the following code to it:在域的根目录中创建一个名为.htaccess的文件,然后向其中添加以下代码:

DirectoryIndex index.php # define the index page.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^customer$ /API/MNP/customerService [NC,L] # Handle requests for "customer"

Put in .htaccess file:放入 .htaccess 文件:

# Redirect main domain to index.php
Redirect / index.php

# Redirect www.myNewProduct.com/customer to www.myNewProduct.com/API/MNP/customerService
Redirect /customer /API/MNP/customerService

尝试这个

RewriteRule ^customer$ http://www.myNewProduct.com/API/MNP/customerService [R]

DirectoryIndex命令添加到您的 htaccess 文件。

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

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