通过.htaccess将URL重定向到自定义URL [英] Redirect URL to custom URL through .htaccess

查看:78
本文介绍了通过.htaccess将URL重定向到自定义URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页www.mypage.com,我想将特定的URL重定向到另一个不存在的URL.

I've got a page www.mypage.com and I want to redirect specific URL to another URL that does not exist.

例如,我希望www.mypage.com/about重定向到www.mypage.com/about-company.php,其中/about不存在-这是我编写的自定义URL.

For example, I want www.mypage.com/about to redirect to www.mypage.com/about-company.php , where /about does not exists - it's a custom URL written by me.

我没有动态URL,只想通过.htaccess告诉此/CUSTOM_URL重定向到此/URL.php".

I've got no dynamic URL, only want to tell through .htaccess "this /CUSTOM_URL redirect to this /URL.php".

URL栏中,它必须显示自定义URL .

我不知道是否可以做到.我一直在寻找它,而我发现的所有东西都是用模式重写动态URL的方法.

I don't know if it can be done. I've looked for it and all I've found are ways to rewrite dynamic URL and so, with patterns.

一个示例就是 Wordpress的永久链接,您可以在其中为每个页面指定所需的URL.

An example of that, would be the Permalinks of Wordpress, where you can specify which URL you want for each page.

推荐答案

尝试将以下内容添加到网站根目录中的.htaccess文件中,以获取您要求的特定示例:通过访问www.mypage.com/about-company.php使用www.mypage.com/about.

Try adding the following to the .htaccess file in the root directory of your site to get the specific example you requested to work: to access www.mypage.com/about-company.php by using www.mypage.com/about.

RewriteEngine on
RewriteBase / 

#if about was requested, server about-company.php
RewriteRule ^about$ about-company.php [L,NC]

如果您想要更一般的规则,例如www.mypage.com/anything用于访问 www.mypage.com/anything.php您可以将上述规则替换为

If you wanted a more general rule e.g. www.mypage.com/anything is used to access www.mypage.com/anything.php you could replace the rule above with

RewriteRule ^(\w+)$ $1.php [L,NC]

或者您可以将所有请求都发送到一个.php文件,该文件决定显示哪个页面,例如.

Or you could have all requests to to a single .php file that decides what page to display e.g.

RewriteRule ^(\w+)$ index.php?page=$1 [L,NC]

这篇关于通过.htaccess将URL重定向到自定义URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆