Mobile Redirect using htaccess - php

My site is:
example.com
My mobile version:
m.example.com
Profile page is
example.com/profile or m.example.com/profile
Or posting page
example.com/posts or m.example.com/posts
Mobile version's url is similar. So, how to redirect the same page?
For example: user go (from facebook) into example.com/posts but he uses mobile device,
so how to redirect via .htaccess to m.example.com/posts
I saw this one threat, but i'm confuse to create right rules.
Thanks :)

Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.
Next, you want to go to your .htaccess file and add something like this:
RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]
The first RewriteCond contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond checks if the host isn't already m.example.com. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1 gets replaced with the first capture group in the regex in the first argument of RewriteRule. The [R] flag is not necessary in this case (links with http:// will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L] flag will stop processing more rules if this rule matches.
See the documentation for more information.

Related

URL Rewritng is working but auto redirect is not for my website

I have used URL rewriting with .htaccess with the below code. Problem is that if we paste the URL then it's working but auto redirection is not working.
.HTACCESS CODE:
Options +FollowSymLinks
RewriteEngine on
RewriteRule Sports-Equipment-Catname-(.*)\.html$ Sports-Equipment.php?Catname=$1
My URLS:
http://example.com/Sports-Equipment.php?Catname=Discus
If I paste direct URL from htaccess is:
http://example.com/Sports-Equipment-Catname-Discus.html
why URLs is not auto-redirected to new pages.
Please help me to fix it.
why URLs is not auto-redirected to new pages.
There is no "automatic" process here. You have no directives that implement the redirect.
My URLs: /Sports-Equipment.php?Catname=Discus
That's the problem, you need to change the actual URLs in your HTML source. This should not be resolved with a "redirect". Otherwise, the user can still "see" the old URL before they click the link. If they copy a link they are copying the "old" URL. Every time the user clicks a link they are redirected - which is slow, not just for the user, but also for your server (uses more resources).
However, you should implement a redirect if you are changing an existing URL structure. But this is to preserve SEO only. For search engines that have indexed the old URLs and for third party backlinks to the old URLs.
For that you would need to add the following "redirect" before the existing rewrites. For example:
# Redirect "old" URL to "new" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Catname=([^&]*)
RewriteRule ^Sports-Equipment\.php$ /Sports-Equipment-Catname-%1.html [QSD,R=301,L]
The condition that checks against the REDIRECT_STATUS env var ensures we only redirect direct requests and not rewritten requests by the later rewrite - thus avoiding a redirect loop.
The QSD flag discards the query string from the initial request.

htaccess redirect to remove duplicate pages

I'm very new to this thing with htaccess rules.
I basically want two things.
Redirect exampledomain.com to www.exampledomain.com. This works, the web host that is used had a FAQ page where they suggested the code I use. What doesn't work is this:
Remove the way to access the "example" page from both "/index.php?p=example" and "/?p=example" to get better search engine ranks. If this means accessing that page from something like "/example", that's a bonus.
I basically want to remove the duplicate links that is caused by the /index.php-variant to get higher search engine ranks. Having prettier links isn't required, just a bonus, the most important thing is that I need to remove the duplicate links.
Here's the code anyway:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^exampledomain.com$
RewriteRule ^(.*)$ http://www.exampledomain.com$1 [R=301]
RewriteRule ^([^/]*)\.html$ /index.php?p=$1 [L]
RewriteRule ^([^/]*)\.html$ /?p=$1 [L]
Have I misunderstood the [L/R=301/*]-flags totally?
EDIT: Ok, to clarify. I want to remove duplicate urls to get higher search engine ranks. What my code above apparently did was to add yet another url to access the same page. Since I only want one way, is there any way to redirect instead of just pointing the "prettified" link to the original?
When I think about it, would a redirection of exampledomain.com/index.php to exampledomain.com do the trick?
Thanks guys so far, sorry for being bad at explaining. :)

Subdomain, htaccess Rewrite and masking URL

Users on my site are creating their own pages, i want to make subdomain with redirect and masking real URL.
I have made RewriteRule:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteCond %1 !=www
RewriteRule ^(.*)$ http://domain.com/site.php?site=%1 [QSA,R=302]
When user enters:subdomain.domain.com he will be trasfered to php script witch finds ID by page name in DB and redirects to:
domain.com/workarea/pages/ID/index.html
I wan't to mask real URL, just to show the URL with subdomain
subdomain.domain.com
but also i want to show all after / when user go to gallery or smth else, like:
subdomain.domain.com/index.html
subdomain.domain.com/gallery.html
but i don`t know how to do that.
I've tried to use [P] flag, but it just redirects to domain.com,
or it is just don`t let the php script to work.
Please help me!
First off, even if set up to have htaccess redirect a subdomain to php; the following remark:
When user enters:subdomain.domain.com he will be trasfered to php
script witch finds ID by page name in DB and redirects to:
domain.com/workarea/pages/ID/index.html
Will ensure that your visitors will see domain.com/workarea/pages/ID/index.html, because - as you say - php redirects visitors to that page. If you want to show the htaccess "masked" url, you need to make sure PHP parses the required HTML and not redirect.
As to the "masked" redirection from htaccess. What is the current behavior?

Restrict access to old files after URL Rewrite

I have a website developed in PHP. I have recently done the URL rewriting which works fine. However, I just found out that my pages with parameters are also accessible. For eg.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
Everything works just fine. However, My site is still accessible using the old parameters. I want if someone types in the old URL should be automatically redirected to Ideally New Page if not then index page. Google and MSN shouldn't access these pages with parameter. Any help will be highly appreciated.
Thanks for your input. Here is more detail to my question.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
The code looks like this.
RewriteRule ^products/(.*)/(.*).html$ index.php?page=product&pid=$1&proTitle=$2 [nc]
Rewrite Works fine. However, if I try to access old URL i.e domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy the page is still accessible and on top of that being crawled by Google and other search engines. I want If someone tries to access this URL, it should direct them to Page Not Found and this should also not be sniffed by any crawlers.
Thanks a lot again for your time and I hope I can get your valuable reply soon.
You need another rule which redirects the browser to the nicer looking URL if the request is made for the ugly looking one. For example:
# need to replace spaces with "-"
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&\ ]+)&pid=([^&\ ]+)&proTitle=([^&\ ]+)
RewriteRule ^ /%1/%2/%3 [L,R=301]

htaccess rule to add locale prefix to URL after locale cookie has been set

I have successfully set up localisation on my website using php gettext, and a browser cookie is set when a visitor chooses their preferred language.
Whilst the pages are shown in the correct language, I want the URL to reflect the currently selected language by prefixing the URL with the language code (not the locale code).
This is what my htaccess file looks like at the moment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en(/?$|/(.*)) $1?locale=en_GB [QSA,DPI,L]
RewriteRule ^fr(/?$|/(.*)) $1?locale=fr_FR [QSA,DPI,L]
RewriteRule ^de(/?$|/(.*)) $1?locale=de_DE [QSA,DPI,L]
RewriteRule ^es(/?$|/(.*)) $1?locale=es_ES [QSA,DPI,L]
RewriteRule ^ru(/?$|/(.*)) $1?locale=ru_RU [QSA,DPI,L]
Let's say a visitor clicks on the French language link on the homepage, the URL displayed to the visitor is mysite.com/fr and the language changes to French.
If they then click on the link for page1.php the URL changes to mysite.com/page1.php and the French version of Page 1 is displayed.
I would like the URL to be shown as mysite.com/fr/page1.php.
I've tried adding the following lines to htaccess, just below the RewriteCond lines, but I'm clearly doing it wrong:
RewriteCond %{HTTP_COOKIE} locale=([a-z]{2}\_[A-Z]{2}) [NC]
RewriteRule ^(.*)$ /%1/$1 [NC,L,QSA]
Note that in the lines above, I am attempting to prefix the URL with the entire locale code (e.g. fr_FR) whereas ultimately what I want is to prefix the URL with just the first two characters of the locale code (e.g. fr).
Thank you in advance.
I don't think you should use .htaccess rewrites for this ..
If a user comes to your site with the URL:
http://mysite.com/page1.php you should figure you what language you want to show to him and redirect him, for instance to: http://mysite.com/en/page1.php - by looking at the browser language accept value for instance.
If a user comes to your site with a URL like:
http://mysite.com/en/page1.php then you don't have to do anything, you know which language to use.
I can strongly recommend you don't use cookies for this. Many sites use cookies for this for absolutely no purpose at all, all they do is send you to the page in whatever language you selected. Completely infuriating if you don't accept cookies by default (which everyone should).
Keep in mind that all your links on your site will always have the language as part of their URL, because if they don't you're back to situation 1 and your user might get switched back to the default language. Using a cookie here will only complicate matters, what if you really did want to switch to the French site after using the English site for a bit?
I guess the bottom line is: don't use cookies in combination with your URL to identify a resource, it becomes very vague which resource exactly you're referring to because cookies are not obvious. The only exception is of course when you're doing some authentication stuff like user accounts and whatnot, but then lack of a cookie should always result in a 'please log in', not something that looks like the thing you wanted, but isn't.

Categories