如何使用 .htaccess 从 url 中隐藏文件名 [英] How to hide filename from url by using .htaccess

查看:26
本文介绍了如何使用 .htaccess 从 url 中隐藏文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 .htaccess 从 url 中完全隐藏文件名.

目前是这样的:mysite.com/文件夹名/文件名

我希望它是这样的:mysite.com/文件夹名

目前我使用 .htacces 文件仅从 url 中删除文件扩展名,我想将其升级为仅显示文件夹名称.我们还没有领导任何关于 .htacces 的事情,所以我很感激非常直接的代码和提示,谢谢!:)

RewriteOptions 继承重写引擎开启RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.php -f重写规则 ^(.*)$ $1.phpRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.html -f重写规则 ^(.*)$ $1.htmlRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.htm -f重写规则 ^(.*)$ $1.htmRewriteCond %{HTTP_HOST} ^.*$RewriteRule ^/?$ "http://tjmediaproductions.com/quiz/" [R=301,L]

解决方案

RewriteOptions inheritRewriteEngine On # 启用 url 重写RewriteCond %{REQUEST_FILENAME} !-d # 如果请求的 uri 不是目录 (!-d)RewriteCond %{REQUEST_FILENAME}.php -f # 如果有一个名为 URI+'.php' 的文件 (-f)RewriteRule ^(.*)$ $1.php # 那么如果uri中有任何东西则将其重写为uri+'.php'

其他都一样
还有那部分我不知道.


<br/>

还有一点和问题
如你所说
目前它是这样的:mysite.com/foldername/filename
我希望它是这样的:mysite.com/foldername
告诉我它背后有什么样的规则?
你想要这个吗

<前>mysite.com/某事

插入

<前>mysite.com/something/something.php

或使用

<前>mysite.com/某事

插入

<前>mysite.com/something/anotherthing.php

如果你想使用第一种方式.
你可以使用这可能是有效的

<前>RewriteCond %{REQUEST_FILENAME} -d # 如果请求的 uri 是目录 (-d)RewriteCond %{REQUEST_FILENAME}.php !-f # 如果没有文件 (!-f)RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # 文件夹名/文件夹名.php

或者这个

<前>RewriteCond %{REQUEST_FILENAME} !-d # 如果请求的 uri 不是目录 (!-d)RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # 文件夹名/文件夹名.php

I was wondering how I could hide filename completetely from url using .htaccess.

Currently it is like: mysite.com/foldername/filename

And I would like it to be like: mysite.com/foldername

At the moment I use .htacces file for removing only file extensios from url and I would like to upgrade it to only show folder name. We have not leader anything about .htacces yet so I would appreciate pretty direct codes and hints, thank you! :)

RewriteOptions inherit
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.htm -f 
RewriteRule ^(.*)$ $1.htm

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http://tjmediaproductions.com/quiz/" [R=301,L]

解决方案

RewriteOptions inherit
RewriteEngine On  # enables url rewriting

RewriteCond %{REQUEST_FILENAME} !-d  # if requested uri is not directory (!-d)
RewriteCond %{REQUEST_FILENAME}.php -f # and if there is a file named URI+'.php' (-f)
RewriteRule ^(.*)$ $1.php # then if there is any thing in uri then rewrite it as uri+'.php'

others are the same
and about the las part idont know.

<br />
<br />

and there is another point and question
as you said
Currently it is like: mysite.com/foldername/filename
And I would like it to be like: mysite.com/foldername
tell me what kind of rule is behind it?
do you want to have this


    mysite.com/something

insted of


    mysite.com/something/something.php

or use


    mysite.com/something

insted of


    mysite.com/something/anotherthing.php

if you want to use first way.
you can use this may be works


    RewriteCond %{REQUEST_FILENAME} -d  # if requested uri is directory (-d)
    RewriteCond %{REQUEST_FILENAME}.php !-f # and if there is not a file (!-f)
    RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # foldername/foldername.php

or maybe this


    RewriteCond %{REQUEST_FILENAME} !-d  # if requested uri is not directory (!-d)
    RewriteRule ^([A-Za-z0-9-]+)/?$ $1/$1.php [NC,L] # foldername/foldername.php

这篇关于如何使用 .htaccess 从 url 中隐藏文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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