大网数据广告活动
网站运营与故障问答

Htaccess w/ Wordpress: rewrite specific path to root domain but keep full path

· 52CE 我爱测

I want http://example.com/foo-bar to display the webpage from http://example.com/ but retain /foo-bar in the address bar. I know it appears as a duplicate of many RewriteRule questions asked here, but none of which I tried retain…

问题原文

原帖链接 · Doruk Karınca · CC BY-SA 3.0

I want

http://example.com/foo-bar

to display the webpage from

http://example.com/

but retain /foo-bar in the address bar. I know it appears as a duplicate of many RewriteRule questions asked here, but none of which I tried retained the first URL in the address bar; they were simple 301/302 redirects.

What I have in .htaccess right now:

Options +FollowSymLinks -MultiViews
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(example.com)$
RewriteRule ^foo-bar http://example.com [NC,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Is this only possible with proxies or am I missing something?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

As you suspect, you need to internally rewrite the request, not externally redirect (301/302). No "proxies" are required. You are almost there with what you have, except that when you specify an absolute URL in the RewriteRule substitution Apache will implicitly trigger an external redirect (despite the docs suggesting that if the domain matches the current host it should be stripped - it doesn't do this in .htaccess in my experience).

Try something like the following:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^foo-bar$ index.html [L]

Unless you are serving mutliple domains then you probably don't need the RewriteCond directive? (The NC flag here is simply a security measure to catch malformed requests.)

I appended a $ (end of string) anchor on the RewriteRule pattern to make it an exact match for "foo-bar" and not a URL that starts "foo-bar". The substitution should reference your DirectoryIndex document (index.html, index.php, or whatever document is loaded by default in the document root). I've removed the NC flag, unless you specifically need a case-insensitive match?

UPDATE: There actually are some Wordpress ...

Ah, WordPress is the "problem" here. In fact, WP already rewrites all requests through index.php in the document root, so these new directives (to "rewrite specific path to root domain but keep full path") are entirely redundant.

WP then uses the URL (the visible path in the address bar) ie. "/foo-bar" in this case, to route the request. Unless "/foo-bar" is defined as a valid URL in WP itself then WP is going to generate a 404 Not Found.

This is something you need to configure in WP itself, not .htaccess.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。