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!
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:
You have to do the check if the bar exists in your
bars.phpscript. 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.It’s redirecting requests to your site with the
wwwmissing from the hostname to the hostname with thewww. So if someone types in:they get redirected to: