的.htaccess使用PHP $ _GET缩短网址网址、htaccess、PHP、_GET

2023-09-02 09:49:17 作者:tū嘫鋒悧の徊yì…

我需要能够从缩短我的网页:

I need to be able to shorten my page from:

mydomain.com/mixtape.php?mixid=(WHATEVER NUMBER)

要:

mydomain.com/m/(WHATEVER NUMBER)

现在通常这不会是太大的问题,我要弄清楚,但becasue在我的.htaccess文件数pre-现有的功能,这是真的很难为这个功能不是不当互动别人。

Now usually this wouldn't be much of an issue for me to figure out, but becasue of a few pre-existing functions in my .htaccess file, it is really hard for this function not to improperly interact with the others.

下面是我的.htaccess文件的当前code(并且没有它可以改变)

Below is the current code of my .htaccess file (AND NONE OF IT CAN CHANGE)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ profile.php?user=$1 [L]

以上,.htaccess文件被短路我

Above, the .htaccess file is shorting my

mydomain.com/profile.php?username=(USERNAME) 

mydomain.com/(USERNAME)

有没有人在那里比能帮助我,能缩短米/的index.php?mixid并没有它的pre-现有功能的冲突?

Is there anyone out there than can help me by being able to shorten the m/index.php?mixid and not have it conflict with the pre-existing function?

推荐答案

prePEND此规则,以你的.htaccess块改写了个人资料网址(打开重写引擎后):

Prepend this rule to your .htaccess block rewriting the profile url (after turning the rewrite engine on) :

RewriteCond $1 ^m/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mix.php?id=$1 [L]

这条规则将现在只用于类似URLS: mydomain.com/m/(任何数字)

That rule will now only be used for URLS like : mydomain.com/m/(WHATEVER NUMBER)

第一行是一个条件,传入的URL必须以 M / 第二行和第三行是条件传入的URL不会重新present实际的文件或文件夹(我们不希望我们的卑微的重写规则,从实际资源阻拦我们)。 在第四行是实际的规则本身女巫使用正则EX pression语法匹配和捕获主机名后出现,并把它传递给 mixtape.php 文件作为名为 ID GET参数。该行还包含 [L] 标志,其中规定,没有规则或改写将发生在当前输入的URL。 The first line is a condition that the incoming URL must start with m/ The second and third lines are conditions that the incoming URL does not represent an actual file or folder (we wouldn't want our humble rewrite rule to block us from a real resource). The fourth line is the actual rule itself witch uses a regular expression syntax to match and capture everything that appears after host name and passes it to the mixtape.php file as a GET parameter called id. This line also contains the [L] flag which states that no more rules or rewriting will occur on the current incoming URL.

在你mix.php文件,你可以用爆炸的方法得到的字符串分割成一个数组:

In your mix.php file you can use the explode method to split the resulting string into an array :

http://example.com/m/foo/bar => `http://example.com/mixtape.php?id=/m/foo/bar

http://example.com/m/foo/bar => `http://example.com/mixtape.php?id=/m/foo/bar

$splitArr = explode('/',$_GET['id']);

$splitArr =>
array (
  0 => 'm',
  1 => 'foo',
  1 => 'bar',
) 

和删除初始 M

array_shift();

然后就只剩下 $ splitArr 包含您的网址的所有部件,拆了 / (斜线)分隔符。

Then you are left with $splitArr containing all the parts of your URL, split with a / (slash) delimiter.

网址 example.com/m/foo/bar 看起来像:

array (
  0 => 'foo',
  1 => 'bar',
) 

现有一个作为现有规则将作用在任何传入URL之前将这一规则是重要的。你有应该出现这样的最后两个规则:

It is important to place this rule before the existing one as the existing rule will act on any incoming URL. The final two rules that you have should appear like this :

RewriteEngine on
RewriteCond $1 ^m/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mix.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ profile.php?user=$1 [L]

关于你的语句:

快Get 收集客户电子邮件地址的9个实用技巧

Regarding your statement :

和无它可以改变

我会认真地建议你考虑落实的第一条规则的微小变化。使得最终网址类似于 mydomain.com/users/(用户名) (因为他们在这里)。在这种情况下,要好得多比过于笼统更具体的(如目前的规则)。你有没有考虑到可能会产生,如果有人是要选择一个用户名,如困惑:

I would seriously recommend that you consider implementing a small change on that first rule. Making the final url something like mydomain.com/users/(USERNAME) (as they do here). In these cases it is much better to be more specific than overly general (as the current rule is). Have you considered the confusion that could be created if someone was to chose a user name such as :

有关 常见问题 主页

虽然完全有效的用户名,这些用户配置文件将是:

While perfectly valid usernames these users profiles would be :

mydomain.com/about mydomain.com/faq mydomain.com/home

这些用户名会阻止,你可能希望保存在您的网站的其他位置重要的网址。我认为这是明显的,为什么这些用户名是不可取的。

Those usernames will block important URLs that you might want to save for other locations on your site. I think it is clear why those user names would be undesirable.