使用的.htaccess干净网址 [英] clean URL using .htaccess

查看:267
本文介绍了使用的.htaccess干净网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重定向我的域名链接。 我想要实现的是: 当用户点击一个链接

mydomain.com/index.php?dir=myfolder

我希望他被重定向到正是这个URL,但浏览器栏应该显示这个网址:

mydomain.com/myfolder

由于我没有获得怎样的联系建立起来(这是一个PHP的网站),我想只用的.htaccess文件做到这一点。

这是我有:

  RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{HTTP_HOST} subdomain.mydomain.com
的RewriteCond%{REQUEST_URI}(。*)/ style.css的[OR]
的RewriteCond%{REQUEST_URI}(。*)/的script.js [OR]
的RewriteCond%{REQUEST_URI}(。*)/ logo.png
重写规则的HTTP(*)://www.subdomain.com% {REQUEST_URI} [R = 301,NC,L]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ /index.php?dir=$1 [L]
 

所以,当我进入mydomain.com/myfolder在浏览器中正常工作,并在地址栏中的URL保持这样。但是,当我进入mydomain.com/index.php?dir=myfolder它的工作原理以及但是URL也保持不变(这不是我想要的)。 我认为我需要以某种方式改变与的.htaccess文件的URL,并把它然后(我已经有)改写成那么PHP的方案。 我做了几个方案,但现在我不知道如何做到这一点。 我希望你明白我的问题,可以帮助我!

感谢您!

 编辑:
 

解决方案(感谢@anubhava):

  RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S / +指数\ .PHP \?DIR =([^ \ s] +)[NC]
重写规则^%1? [R = 301,L]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ /index.php?dir=/$1 [L]
 

解决方案

就在你现有的.htaccess文件的末尾添加这条规则:

 的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S / +指数\ .PHP \?DIR =([^ \ s] +)[NC]
重写规则^%1? [R = 301,L]
 

I'm trying to redirect the links on my domain. What I'm trying to achieve is: When the user clicks on a link to

mydomain.com/index.php?dir=myfolder

I want him to be redirected to exactly this URL but the browser bar is supposed to show this URL:

mydomain.com/myfolder

Since I don't have access to how the links are established (it's a php-site) I am trying to do this with just the .htaccess-file.

This is what I have:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain.mydomain.com
RewriteCond %{REQUEST_URI} (.*)/style.css [OR]
RewriteCond %{REQUEST_URI} (.*)/script.js [OR]
RewriteCond %{REQUEST_URI} (.*)/logo.png
RewriteRule (.*) http://www.subdomain.com%{REQUEST_URI} [R=301,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?dir=$1 [L]

So when I enter "mydomain.com/myfolder" into the browser it works fine and the URL in the address-bar stays like this. But when I enter "mydomain.com/index.php?dir=myfolder" it works as well but the URL also stays the same (which is not what I want). I assume that I need to somehow change the URL with the .htaccess-file and have it then (as I already have) rewritten to then php-scheme. I did several approaches now but I have no idea how to do this. I hope you understand my question and can help me!

Thank you!

EDIT:

The solution ( thanks to @anubhava):

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?dir=([^\s]+) [NC]
RewriteRule ^ %1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?dir=/$1 [L]

解决方案

Just append this rule at the end of your existing .htaccess file:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?dir=([^\s]+) [NC]
RewriteRule ^ %1? [R=301,L]

这篇关于使用的.htaccess干净网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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