简体   繁体   English

如何使用htacess文件(mod_rewrite)模拟目录?

[英]How to simulate directories with a htacess file (mod_rewrite)?

what I try to do is to simulate directories with the help of a htaccess file. 我试图做的是借助htaccess文件模拟目录。 I have a website with a file like this: 我有一个带有以下文件的网站:

http://www.domain.com/filename.php?t=yeah-a-title-2014 http://www.domain.com/filename.php?t=yeah-a-title-2014

Now, I would like to rewrite the URL above to the following: 现在,我想将上面的URL重写为以下内容:

http://www.domain.com/directory1/yeah-a-title-2014/ http://www.domain.com/directory1/yeah-a-title-2014/

If a visitor enters one of the two URLs, he should see the second one in his address bar but the content of the filename.php?t=yeah-a-title-2014 should be displayed. 如果访客输入两个URL中的一个,则他应该在地址栏中看到第二个URL,但是应该显示filename.php?t = yeah-a-title-2014的内容。

I have no idea how to realize this. 我不知道该如何实现。 Any ideas? 有任何想法吗?

This is better known as SEO-urls (search engine optimized), SEF-urls (search engine friendly), fancy urls and a couple more of those terms. 这就是众所周知的SEO网址(已优化搜索引擎),SEF网址(对搜索引擎友好),精美的网址以及其中的两个或更多个术语。 The basic problem with these kind of constructions, is that they cause an infinite loop if not implemented correctly, and therefore usually the THE_REQUEST trick is used, because %{THE_REQUEST} is always equal to the request, even if the url is rewritten, which in turn prevents the external redirect from matching if the internal rewrite matches. 这种构造的基本问题是,如果执行不正确,它们会导致无限循环,因此通常使用THE_REQUEST技巧,因为即使重写了url, %{THE_REQUEST}也总是等于请求,如果内部重写匹配,则依次阻止外部重定向匹配。

#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /filename\.php\?t=(.*)\ HTTP
RewriteRule ^ /directory1/%2/ [R,L]
#Change [R,L] to [R=301,L] after ALL rules do what you want them to do, and before your site goes live

#Internal rewrite
RewriteRule ^directory1/([^/]+)/?$ /filename.php?t=$1 [L]

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

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