简体   繁体   English

使用htaccess将目录重定向到文件?

[英]Redirect directory to file using htaccess?

I'm trying to do something like this... 我正在尝试做这样的事情...

Redirect mysite.com/directory/ To mysite.com/directory/do 将mysite.com/directory/重定向到mysite.com/directory/do

But ONLY when "/directory/" is opened without pointing to a file. 但是仅当打开“ / directory /”而不指向文件时。 I am aware that "DirectoryIndex" can help with this, but I want the file's name (which is "do") to appear in the url the users sees. 我知道“ DirectoryIndex”可以帮助解决此问题,但是我希望文件名(即“ do”)出现在用户看到的url中。 Is there a way to accomplish this with .htaccess..? 有没有办法用.htaccess ..完成此操作?

Thanks in advance! 提前致谢!

If I've understood correctly, you want something like this: 如果我正确理解,则需要以下内容:

RewriteEngine On
# Only redirect if we're in /directory/ and *not* pointing at
# a file that already exists in the filesystem
RewriteCond %{REQUEST_URI} ^/directory/
RewriteCond %{REQUEST_URI} !-f
RewriteRule . /directory/do [R,L]

Alternatively, if you meant "when the user accesses the URL mysite.com/directory/ fullstop", which on second thought you may have, this will work as well: 或者,如果您的意思是“当用户访问URL mysite.com/directory/fullstop时”(第二想您可能会知道),则此方法也可以工作:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/directory/$
RewriteRule . /directory/do [R,L]

Edit: In case /do isn't an actual file: 编辑:如果/ do不是实际文件:

RewriteEngine On

RewriteCond %{REQUEST_URI}  ^/directory/
RewriteCond %{REQUEST_URI} !^/directory/do$
RewriteCond %{REQUEST_URI} !-f
RewriteRule . /directory/do [R,L]
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/redeem/surveys/$ [NC]
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ /redeem/surveys/do$1 [L,R=301]

That works...but can it be better...? 那行得通...但是会更好吗...?

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

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