.htaccess redirect based on language with wildcard - php

I have this on .htaccess for browser redirect based on language, to avoid another php code.
What i want is if the user enters www.example.com/ (wildcard after slash) the browser redirects to en.example.com/wildcard.
what i have so far is this for each language:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^en.example.com$ [NC]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule .* http://en.example.com [R,L]
this works well but if i enter www.example.com/contact it will redirect to en.example.com and not ex.example.com/contact
can you see what's missing?

You can capture .* and pass that to the rewrite URL in the RewriteRule definition as:
RewriteRule ^(.*) http://en.example.com/$1 [R,L]
Reference document: URL Rewriting Guide

Related

redirect url exact match with htaccess like case sensitive

redirect url exact match with htaccess not redirect if query string or anything after index.php
Redirect This Page: https://example.com/demo/index.php
To Page: https://example.com/ (home page)
But Do Not redirect: https://example.com/demo/index.php/*
Do NOT redirect: https://example.com/demo/index.php/password
Do NOT redirect https://example.com/demo/index.php?m=page
not redirect if any other combination
only redirect https://example.com/demo/index.php to https://example.com/
this script not working
RewriteEngine On
RewriteBase /
RewriteRule ^demo/index.php /demo/index.php?m=page[L,NC,END]
RewriteRule ^demo/index.php$ https://example.com/ [L,R=301]
only redirect https://example.com/demo/index.php to https://example.com/
If you only want to redirect that exact URL, without a query string then you need to reverse your existing rules and include an additional condition that checks that the QUERY_STRING is empty. The RewriteRule only matches against the URL-path, which excludes the query string.
You are also missing a space before the flags argument in the first rule.
For example:
# Redirect exact URL to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ / [R=302,L]
# Redirect other URLs with query string OR path-info
RewriteRule ^demo/index\.php /demo/index.php?m=page [NC,L]
You don't need L and END.
Test with 302, and change to 301 only once you have confirmed it works OK. To avoid caching issues.
You may use these rules in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+demo/index\.php\s [NC]
RewriteRule ^ / [L,R=301]
RewriteRule ^demo/index\.php$ $0?m=page [QSA,NC,L]
This was created by htaccess redirect generator I use very often.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]
Maybe "?" sign is redundant because of empty query string match.
Not sure, but You also can try to add
RewriteCond %{REQUEST_METHOD} !=POST
in case You can't log in. All together:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]

htaccess cant get rewrite seo friendly url

hi i have use many different htaccess codes, but i cant get rewrite to work.
i want this url
https://domain.com/category.php?cat=firm
to look like this url
https://domain.com/category/firm
this is my latest attempt
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www\.seoboost\.no$
RewriteRule ^/?$ "https\:\/\/seoboost\.no\/" [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index\.php$ / [R=301]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301]
i have try to delete all in my htaccess and only have this code
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
but it still not working, is my server or what am i doing wrong??
Try replacing your .htaccess with
RewriteEngine On
RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
which will redirect yoursite.com/category/12 to yoursite.com/category.php?cat=12 internally, and the user will never see the "ugly" url. Then inside your category.php file you can access the cat by $category_id = $_GET['cat']
A simple anchor tag looks like this:
Item 12
Pulling from Justin Lurman's answer here, the following should work:
RewriteEngine On
# redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
RewriteCond %{THE_REQUEST} \s/category\.php\?cat=(.+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
# internally rewrite "/category/..." to "/category.php?cat=..."
RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
As Justin noted in that answer to a similar question, you need to confirm that htaccess files are enabled/allowed in your Apache configuration and that mod_rewrite is enabled.
Additionally, I am sure you are aware, but just in case you are not, after implementing this new htaccess redirect rule, you should change all of the links on your webpages to use clean/friendly URLs. By doing so, neither your users nor a search engine crawler will access the "ugly" URLs unless they access them directly (via a bookmark or a saved link).

Redirect all non-www to www except one subdirectory using htaccess.

If want to redirect all non-www requests to my site to the www version. All I need to do is add the following code to my .htaccess file.
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule .* http://www.mydomain.com%{REQUEST_URI} [R=301,L]
The problem is that when I write for example mydomain.com/products-1 (hidden URL for mydomain.com/products?category=1), all parameters become visible, even though they are specified on the .htaccess file, and I get an output url (after the redirect) of www.mydomain.com/products-1?category=1
How can I fix this? Is there any kind of problems with the .htaccess code above?
Try Changing your RewriteRule:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I prefer this because it will catch all *.domain.com. If that is not what you want, then use your original HTTP_HOST rule.
If my logic is working this morning, this rule should rewrite any requests that do not match:
www.example.com
and do not contain
/subfolder
to
www.domain.com/URI

.htaccess rewrite to produce clean url

Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url

I want to redirect old url to new rewrite url using htaccess or php

I have a problem since i using rewrite url..
MY OLD URL:
Website.com/index.php?act=appdetail&appid=oWV
New Rewrite URL
http://website.com/angry_birds_rio-appdetail-oWVi.html
But all my old url are indexed in google and if any one come to my website its display the old URL and google also INDEXED the NEW URL. its make duplicate page on website problem.
Let me know the solution
My rewrite URL htaccess
RewriteEngine On
RewriteRule ^([^-])-([^-])-([^-])-([^-])-([^-]*).html$ index.php?appanme=$1&act=$2&appid=$3&page=$4&cat=$5 [L]
RewriteRule ^([^-])-([^-])-([^-])-([^-])-([^-])-([^-]).html$ index.php?appanme=$1&act=$2&appid=$3&page=$4&cat=$5&sString=$5 [L]
RewriteRule ^([^-])-([^-])-([^-]*).html$ index.php?appanme=$1&act=$2&appid=$3[L]
Here is your .htaccess file:
RewriteEngine on
RewriteRule ^/index.php?act=appdetail&appid=oWV$ http://website.com/angry_birds_rio-appdetail-oWVi.html [R=301,L]
You'll need to inform to web crawlers about the redirection, you cando it with a 301 code.
Appears the rule are .htaccess based; you need an additional set of rules to permanently redirect (301) BROWSER/CRAWLER requests for the index.php pages, if a set of CGI arguments are present, to the appropriate alias, this will tidy up Google in a few weeks. Then your rules above e.g.
RewriteEngine On
RewriteBase /
#Permanently redirect BROWSER requests for the index.php?xxx to the appropriate page alias:
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+)&page=([^&]+)&cat=([^&]+)&sString=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3-%4-%5-%6.html [R=301,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+)&page=([^&]+)&cat=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3-%4-%5.html [R=301,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3.html [R=301,L]
# Followed by: YOUR RULES FROM ABOVE
Note:
1) There appears to be a typo in YOUR second rule: sString=$6 NOT sString=$5
2) The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

Categories