如何我重定向用的.htaccess缺陷 [英] flaws in how I'm redirecting with .htaccess

查看:206
本文介绍了如何我重定向用的.htaccess缺陷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做一些不同的事情和我的.htaccess文件,但由于它仍然有些新的给我,我有一些制造麻烦的一切做我想做的。下面是我有什么用?麻烦

1)我要重定向到我的移动网站为移动设备,不同在于,所述移动设备正在运行的Opera Mini的情况。我试着解决,但它仍然重定向的Opera Mini。这是我必须重定向(基于code我从该网站得到的。这code工作)

 #检测,如果用户在移动设备上
的RewriteCond%{HTTP_USER_AGENT}安卓| AvantGo公司|黑莓|外套|华宝|伊莱恩|非洲小狐| hiptop | iemobile | IP(磨练| OD)|虹膜|点燃| LGE \ |的Maemo | MIDP | MMP |歌剧\ MOBI |掌(\ OS)?|电话| P(IXI | RE)\ / |包机|口袋| PSP |塞班|的Treo |。达\(浏览器|链接)|沃达丰| WAP WINDOWS \(CE |电话)| XDA | xiino [ NC,OR]
#如果用户是移动的,重定向到移动网站,preserving的URL的其余部分
#例如,http://www.triadbarspecials.com/barname重定向到http://m.triadbarspecials.com/barname
重写规则^(。*)$ http://m.triadbarspecials.com/$1 [L,R = 302,NC]
 

所以,我想确保Opera Mini的总是指向我的电脑版,所以我增加了以下code:

 #检测,如果我们是在Opera移动
的RewriteCond%{HTTP_USER_AGENT}歌剧\迷你
#如果是这样,请确保用户在主站点
重写规则^(。*)$ http://www.triadbarspecials.com/$1 [L,R = 302,NC]
 

任何人都可以看到这是怎么回事那里,是造成这不直接到主站点?

2)我有我的网站设置,使用户可以键入URL的.COM /部分之后酒吧的名称,如果我的网站上,意志的.htaccess路由条存在用户到正确的页面。 例如, triadbarspecials.com/justinsbar相同triadbarspecials.com/bars.php?barname=justinsbar

我有我的错误文档无法正常工作的问题。如果用户键入不上我的网站有一个酒吧的名字,然后是网址还是定向到网页,但不显示动态内容,因为没有酒吧这个名字。用户还涉及到该网页的任何年代后 triadbarspecials.com / 键入只要键入不存在的URL。这里的code我有这个...

 #以下允许的URL被输入刚才在酒吧的名字
#例如,http://www.triadbarspecials.com/bars.php?barname=barname变为http://www.triadbarspecials.com/barname
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ bars.php?barname = $ 1 [L]
 

我可以看到这个code看起来普遍,所以希望有人能帮助支撑这件事,并把事情重定向正常。下面是我对错误文档

 的ErrorDocument 400 /error.php?error=400
ErrorDocument的401 /error.php?error=401
ErrorDocument的403 /error.php?error=403
ErrorDocument的404 /error.php?error=404
ErrorDocument的500 /error.php?error=500
 

3)我有我的.htaccess文件这code,我已经完全忘记了它的作用。我不希望只是删除它,所以希望有人能解释它做什么...

 的RewriteCond%{HTTP_HOST} ^ triadbarspecials.com [NC]
重写规则^ $ HTTP(*):#www.triadbarspecials.com / $ 1 [R = 301,NC]
 

在此先感谢任何人谁可以帮助!

解决方案
  1. 根据的用户代理链接的 Opera Mini的包含字符串的的Opera Mini 的和剧的mobi的,而你对匹配,第二个在你的第一个重定向。这意味着你的Opera Mini的装置被重定向到移动网站和第二条规则将永远不会得到应用。你应该摆脱第二重定向并结合2条件:

     #检测,如果用户在移动设备上
    的RewriteCond%{HTTP_USER_AGENT}安卓| AvantGo公司|黑莓|外套|华宝|伊莱恩|非洲小狐| hiptop | iemobile | IP(磨练| OD)|虹膜|点燃| LGE \ |的Maemo | MIDP | MMP |歌剧\ MOBI |掌(\ OS)?|电话| P(IXI | RE)\ / |包机|口袋| PSP |塞班|的Treo |。达\(浏览器|链接)|沃达丰| WAP WINDOWS \(CE |电话)| XDA | xiino [ NC]
    #只要它不是的Opera Mini
    的RewriteCond%{HTTP_USER_AGENT}!歌剧\迷你[NC]
    #如果用户是移动的,重定向到移动网站,preserving的URL的其余部分
    #例如,http://www.triadbarspecials.com/barname重定向到http://m.triadbarspecials.com/barname
    重写规则^(。*)$ http://m.triadbarspecials.com/$1 [L,R = 302,NC]
     

  2. 您需要做的检查,如果扎在你的 bars.php 脚本存在。在那个脚本,如果所要求的酒吧在 barname 参数不存在,然后重定向浏览器 /error.php?error=404 。你不能在mod_rewrite的做到这一点,因为严格的规则,不知道该barname是否存在于您的数据库或没有。 mod_rewrite的没有内在的访问你的数据库。

  3. 它请求重定向到您的网站使用 WWW 从主机名失踪的主机名的的的 WWW 。所以,如果有人类型:

      http://triadbarspecials.com/somebar
     

    他们重定向到:

      http://www.triadbarspecials.com/somebar
     

I'm trying to do a few different things with my .htaccess file, but since it's still somewhat new to me I'm having some trouble making everything do what I want. Here's what I'm having trouble with...

1) I want to redirect to my mobile site for mobile devices, except in the case that the mobile device is running Opera Mini. I've tried to resolve that, but it's still redirecting Opera Mini. Here's what I have to redirect (based on code I got from this site. this code works)

# Detect if the user is on a mobile device
RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ mobi|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC,OR]
# If user is mobile, redirect to mobile site, preserving the remainder of the URL
# For example, http://www.triadbarspecials.com/barname redirects to http://m.triadbarspecials.com/barname
RewriteRule ^(.*)$ http://m.triadbarspecials.com/$1 [L,R=302,nc]

So, I wanted to make sure that Opera Mini is always directed to my Full Website, so I added the following code:

# Detect if we're in Opera Mobile
RewriteCond %{HTTP_USER_AGENT} opera\ mini
# If so, make sure user is on the main site
RewriteRule ^(.*)$ http://www.triadbarspecials.com/$1 [L,R=302,nc]

Can anyone see what's going on there that's causing that to not direct to the main site?

2) I have my site set up so that users can type the name of a bar after the .com/ part of the URL, and if that bar exists on my site, .htaccess will route the user to the correct page. For example, triadbarspecials.com/justinsbar is the same as triadbarspecials.com/bars.php?barname=justinsbar

I'm having issues with my error documents not working properly. If the user types the name of a bar that doesn't exist on my site, then the URL still directs to the page, but no dynamic content is displayed since there's no bar by that name. User is also directed to that page for ANYTHING that's typed after triadbarspecials.com/ as long as the URL they type does not exist. Here's the code I have for this...

# The following allows for URL's to be typed as just the bar's name
# for example, http://www.triadbarspecials.com/bars.php?barname=barname is changed to     http://www.triadbarspecials.com/barname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ bars.php?barname=$1 [L]

I can see that this code looks generalized, so hopefully someone can help shore this up and get things redirecting properly. Here's what I have for the error docs

ErrorDocument 400 /error.php?error=400
ErrorDocument 401 /error.php?error=401
ErrorDocument 403 /error.php?error=403
ErrorDocument 404 /error.php?error=404
ErrorDocument 500 /error.php?error=500

3) I have this code in my .htaccess file, and I've completely forgotten what it does. I don't want to just delete it, so hopefully someone can explain what it does...

RewriteCond %{HTTP_HOST} ^triadbarspecials.com[nc]
RewriteRule ^(.*)$ http:#www.triadbarspecials.com/$1 [r=301,nc]

Thanks in advance to anyone who can help!

解决方案

  1. According to this link the user-agent for Opera Mini contains both the strings "opera mini" and "opera mobi", and you're matching against the second one in your first redirect. That means your "Opera mini" device gets redirected to the mobile site and the second rule will never get applied. You should get rid of the second redirect and combine the 2 conditions:

    # Detect if the user is on a mobile device
    RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ mobi|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC]
    # As long as it's NOT opera mini
    RewriteCond %{HTTP_USER_AGENT} !opera\ mini [NC]
    # If user is mobile, redirect to mobile site, preserving the remainder of the URL
    # For example, http://www.triadbarspecials.com/barname redirects to http://m.triadbarspecials.com/barname
    RewriteRule ^(.*)$ http://m.triadbarspecials.com/$1 [L,R=302,nc]
    

  2. You have to do the check if the bar exists in your bars.php script. Inside that script, if the bar requested by the barname parameter doesn't exist, then redirect the browser to /error.php?error=404. You can't do this strictly within mod_rewrite because the rule has no idea whether the barname exists in your database or not. Mod_rewrite doesn't have inherent access to your database.

  3. It's redirecting requests to your site with the www missing from the hostname to the hostname with the www. So if someone types in:

    http://triadbarspecials.com/somebar
    

    they get redirected to:

    http://www.triadbarspecials.com/somebar
    

这篇关于如何我重定向用的.htaccess缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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