Redirect URL to custom URL through .htaccess

2019-01-28 21:09发布

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

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.

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

In the URL bar, it must show the custom 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.

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

3条回答
Deceive 欺骗
2楼-- · 2019-01-28 21:21

You are looking for rewrite rules.

Some reading on that: Apache Rewrite rules

查看更多
Root(大扎)
3楼-- · 2019-01-28 21:36

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]

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]

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]
查看更多
混吃等死
4楼-- · 2019-01-28 21:36

Building on Ulrich Palha's comment, I found that this worked for me as an .htaccess file to provide alternative redirects with masking to a couple of my own .php pages:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(optionOne$|optionOneWithEndingSlash/$|optionTwo$|etCetera/$) ppl/peaceseeker.php [L] [NC]
RewriteRule ^(donny$|donny/$|donnycallahan$|donnycallahan/$) ppl/donnycallahan.php [L] [NC]

(I mention this because Ulrich's solution caused a problem for me when people typed example.com/about/ (with an ending slash)).

Thanks, Ulrich. If anyone knows how to modify Ulrich's .php-suffix Rule to descend recursively into any number of directories, I'd appreciate a comment!

As a temporary solution for anyone who needs to descend into directories, this worked for me:

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

The second command will handle the first subdirectory of all directories in the root folder of your website.

You can handle 3 levels of subdirectories, or 4, or 5, or 10, by just adding more instances of "/(\w+)" before the $ (one per new level of subdirectories), and then adding /$3, /$3/$4, /$3/$4/$5, or whatever is necessary immediately before ".php" in the Rule. I only needed 2 levels of directories (for now), so I'm good with what I posted; that solution is simple enough.

I'd still like a recursive solution if possible.

查看更多
登录 后发表回答