简体   繁体   English

重定向 php,同时保留 URL

[英]Redirect php while keeping URL

I am attempting to redirect a URL in a very common method, however, I can not figure this out.我正在尝试以一种非常常见的方法重定向 URL,但是,我无法弄清楚这一点。 I have read article after article and can not make any progress.我已经阅读了一篇又一篇文章,但无法取得任何进展。 I am simply trying to have a URL such as 'domain.com/xx/xxxxxxxxxx' redirect in php to '/index.php?first=xx&last=xxxxxxxxxx'.我只是想在 php 中将 URL 之类的 'domain.com/xx/xxxxxxxxxx' 重定向到 '/index.ZE1BFD762321E409CEE4AC0B6E84196xxx'CZ? I have attempted several apache mod_rewrites and can get the URL to remain as desired in the browser, but when I look at $_SERVER['REQUEST_URI'] in index.php, it is not showing up as desired.我已经尝试了几个 apache mod_rewrites 并且可以让 URL 在浏览器中保持所需的状态,但是当我查看 index.ZE1BFD762321E409CEE4AC 中的 $_SERVER['REQUEST_URI'] 时,它没有按预期显示。 Instead it is showing the URI that was entered instead of the rewrite.相反,它显示的是输入的 URI,而不是重写。 It is also always adding the 'xx' portion of the URL.它还总是添加 URL 的“xx”部分。 So for example:例如:

URL: domain.com/ab/1234567890
PHP: ab/1234567890
     ab/include/file.css
     ab/include/another.css

Getting: $_SERVER['REQUEST_URI'] = domain.com/ab/1234567890
Desired: $_SERVER['REQUEST_URI'] = domain.com/index.php?first=ab&last=1234567890

There is no 'ab' directory, and it should be looking for files like 'include/file.css', etc.没有'ab'目录,它应该寻找'include/file.css'等文件。

Here are some of the redirects I've tried:以下是我尝试过的一些重定向:

 RewriteRule ^([a-zA-Z0-9/]+)$ index.php?first=$1

 RewriteCond %{QUERY_STRING} /([a-z][a-z])/([a-zA-Z0-9]+)
 RewriteRule ^/([a-z][a-z])/([a-zA-Z0-9]+)$ index.php?first=$1&last=$2 [L]

 RewriteCond   %{QUERY_STRING}           ^[a-z][a-z]/[a-zA-Z0-9]$
 RewriteRule   ^[a-z][a-z]/[a-zA-Z0-9]+$         index.php      [L,R=301]

I have tried many others and nothing I try seems to work.我已经尝试了很多其他方法,但我尝试的任何方法似乎都不起作用。 Perhaps it is because I am just unfamiliar with apache redirects and how this interacts with PHP.也许是因为我不熟悉 apache 重定向以及它如何与 PHP 交互。 I am good at PHP, just not apache, therefore it may be a situation where I just don't know the terms to look for...我擅长 PHP,只是不擅长 apache,因此可能是我不知道要查找的术语的情况...

Any help would greatly be appreacited!任何帮助将不胜感激!

UPDATE更新

My current apache config is:我当前的 apache 配置是:

RewriteEngine On
RewriteRule ^[a-z][a-z]/([a-z/]+.css)$ $1   [L]
RewriteRule ([a-z][a-z])/([a-zA-Z0-9]+) index.php?first=$1&second=$2    [L]

I have tried switching orders, but that doesn't seem to make any difference.我尝试过切换订单,但这似乎没有任何区别。 If I check the $_SERVER['QUERY_STRING'] variable value in PHP, it is indeed formatting it correctly for the original URL in the browser:如果我检查 PHP 中的 $_SERVER['QUERY_STRING'] 变量值,它确实在浏览器中为原始 URL 正确格式化:

Browser: domain.com/ab/0123456789
$_SERVER['QUERY_STRING']: first=ab&second=0123456789

However, for all the external files that the page is requesting, those are not being redirected correctly and the PHP script is outputting the wrong values:但是,对于页面请求的所有外部文件,这些文件没有正确重定向,并且 PHP 脚本输出错误的值:

Browser: domain.com/ab/include/style.css
$_SERVER['QUERY_STRING']: first=ab&second=include

This should be reporting as:这应该报告为:

$_SERVER['QUERY_STRING']: include/style.css

It appears that the first RewriteRule is not getting selected for some reason for the css files.似乎由于某种原因没有为 css 文件选择第一个 RewriteRule。 This is also happening with js files (obviously because no rule has been set for them yet).这也发生在 js 文件中(显然是因为还没有为它们设置规则)。

It also makes no difference if I use relative paths or hard paths...如果我使用相对路径或硬路径也没有区别......

RESOLUTION解析度

After continued effort getting the apache redirects working, I had to add a specific line for each external source location (no clue why the original RegEx statement from 'Dont Panic' didn't work).在继续努力使 apache 重定向工作后,我不得不为每个外部源位置添加一个特定的行(不知道为什么“不要恐慌”中的原始 RegEx 语句不起作用)。 Here is what my .htaccess file looks like now:这是我的 .htaccess 文件现在的样子:

# these are for the external file requests
RewriteRule [a-z][a-z]/images/([a-zA-Z0-9_\.]+)$ /images/$1 [L]
RewriteRule [a-z][a-z]/styles/([a-zA-Z0-9_\.]+)$ /styles/$1 [L]

# these are for the main request for the bootstrap php file
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z][a-z])/([a-zA-Z0-9]+) index.php?first=$1&second=$2 [L]

Also of note: if you have any external files that are not created in this example (eg styles/non_existent.css), the last 'RewriteRule' above will execute and basically run the index.php file causing errors for your site.另请注意:如果您有任何未在此示例中创建的外部文件(例如 styles/non_existent.css),则上面的最后一个“RewriteRule”将执行并基本上运行 index.php 文件,从而导致您的站点出错。 So make sure that you create (or remove references to) external files - even if they are blank!!!因此,请确保您创建(或删除对)外部文件的引用——即使它们是空白的!!!

Thanks for all your help 'Dont Panic'!感谢您的所有帮助“不要惊慌”!

As I understand it, these are your requirements:据我了解,这些是您的要求:

  • You want to leave the originally requested URL in the browser, ie not an external, visible-to-the-visitor 301-type redirect.您希望将最初请求的 URL 留在浏览器中,即不是外部的、对访问者可见的 301 类型重定向。 In other words, map the URL in the browser to something else on the filesystem, transparently.换句话说,map URL 在浏览器中对文件系统上的其他内容是透明的。

  • The incoming request includes 2 segments which can be matched with regular expressions;传入的请求包括2个可以用正则表达式匹配的段;

  • You want to be able to access those 2 directory elements of the request as PHP parameters;您希望能够以 PHP 参数的形式访问请求的这两个目录元素;

An Apache internal redirect will do this. Apache 内部重定向将执行此操作。 The first example on the mod_rewrite examples page in the Apache docs describes this (though you probably don't need the [PT] flag used there, which allows further handling of the mapped URI). Apache 文档中mod_rewrite 示例页面上的第一个示例描述了这一点(尽管您可能不需要在那里使用[PT]标志, 它允许进一步处理映射的 URI)。 I think the following should do what you need:我认为以下应该做你需要的:

RewriteEngine on
RewriteRule ([a-z][a-z])/([0-9]+) index.php?first=$1&last=$2 [L]

This will handle an incoming request like domain.com/ab/1234567890 , and map it - transparently, invisibly - to index.php?first=ab&last=1234567890 .这将处理传入的请求,如domain.com/ab/1234567890和 map - 透明地,不可见地 - 到index.php?first=ab&last=1234567890 The visitor does not see any redirect, only the original URL in their browser.访问者在他们的浏览器中看不到任何重定向,只看到原始的 URL。 You can still access the different elements of both the external request and the internal handling of it through $_SERVER :您仍然可以通过$_SERVER访问外部请求和内部处理的不同元素:

...
[QUERY_STRING] => first=ab&last=1234567890
[REQUEST_URI] => /ab/1234567890
[SCRIPT_NAME] => /index.php
[PHP_SELF] => /index.php
...
[argv] => Array
    (
        [0] => first=ab&last=1234567890
    )

So for example you can use $_GET['first'] in your code to find ab .因此,例如,您可以在代码中使用$_GET['first']来查找ab

UPDATE更新

To answer extra questions added in the comments - to reference your CSS/JS from index.php , the simplest and most efficient option would be to use absolute references:要回答评论中添加的额外问题 - 从index.php引用您的 CSS/JS,最简单和最有效的选择是使用绝对引用:

<link href="/css/some.css" rel="stylesheet">

If you can't do that, and instead want relative links to work, you could try another internal remap.如果您不能这样做,而是希望相对链接起作用,您可以尝试另一个内部重新映射。 Note that an external redirect (like you've shown in the comments) with [R=301] will be slower, as it is really is a whole new request and response.请注意,带有[R=301]的外部重定向(就像您在评论中显示的那样)会更慢,因为它确实是一个全新的请求和响应。

Assuming you have CSS files like include/file.css , where the include/ directory is in the same directory as index.php , then a relative CSS reference in index.php like: Assuming you have CSS files like include/file.css , where the include/ directory is in the same directory as index.php , then a relative CSS reference in index.php like:

<link href="include/some.css" rel="stylesheet">

will - with the first redirect rule above in place - actually generate a request for ab/include/some.css .将 - 使用上面的第一个重定向规则 - 实际上会生成对ab/include/some.css的请求。 To remap that to your real file, the following internal redirect rule works:要将其重新映射到您的真实文件,请使用以下内部重定向规则:

RewriteRule ^[a-z][a-z]/([a-z/]+.css)$ $1 [L]

Note that you need to be careful now that the rules don't overlap or interfere with each other, so I've added [L] to both, and also added some anchors to the CSS regex.请注意,您现在需要注意规则不会重叠或相互干扰,因此我在两者中都添加了[L] ,并且还在 CSS 正则表达式中添加了一些锚点。

Url will always change once page changes.一旦页面更改,Url 将始终更改。 Ways to circumvent this is by using javascript and load url content in a div or using iframe in html to display the target page.规避此问题的方法是使用 javascript 并在 div 中加载 url 内容,或在 ZFC35FDC777777 中使用 iframe 中的 iframe 到目标显示页面。

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

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