Rewrite language URI path to other path - .htaccess

I have a little trouble to implement a htaccess rewrite rule. I just need rewrite any language ID passed as URI to other URI inside my MVC framework.
Basically, when someone write something like:
http://www.example.com/en-us or http://www.example.com/pt-br
The URL to rewrite must be:
http://www.example.com/locale/to/en-us and http://www.example.com/locale/to/pt-br
I already try to implement something like that:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/(pt-br|en-us)
RewriteRule (.*) /locale/to/$1
But doesn't work, I can't get a rewrite using this code.

The final solution to fix my question is:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/((pt-br|en|en-us).*)
RewriteRule (.*) /locale/to/$1 [R=301,L]
Using this rule, the rewrite will work as espected.

Related

Redirect htaccess stuggles

I would like to make a rewrite rule for my website but I cannot seem to get the proper code in order to make this work.
The current URL of my website is looking like http://www.mohanadarafe.io/JSON/json.html
I want it to be: http://www.mohanadarafe.io/json
I have tried the following code but it does not seem to work:
RewriteEngine On
RewriteRule ^\.html$ /json [L]
Any idea how to fix this?
You have it reversed. Have it like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /JSON/json\.html [NC]
RewriteRule ^ /json [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^json/?$ JSON/json.html [L,NC]
Then you can have your URL as: http://www.mohanadarafe.io/json

Rewrite and redirect with modrewrite

I am simply trying to rewrite and redirect automatically this:
From mysite.com/sub/index.php?channel=rai1
To mysite.com/sub/channel-rai1.html
I've tried with this:
RewriteRule ^sub/channel-([^/]*)\.html$ /sub/index.php?channel=$1 [L]
but the problem is that the redirection does not happen. Why?
Changing your rewrite rule to:
RewriteRule ^sub/channel-([^/]*)\.html$ /sub/index.php?channel=$1 [R=301]
will work. You were writing the rule, but you weren't telling it to change it in the browser.
[L] tell it to stop applying any rules that follow. [R=301] will do the actual redirection of the url to the new one and tell browers/search engines that the new url is the permanent one to use. You can use [R=301, L] which will combine the two.
EDIT:
I read the question wrong, my apologies
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /sub/index\.php\?channel=(.*)&data=(.*)\ HTTP
RewriteRule ^sub/index.php /sub/channel-%2-%3.html? [R,L]
#Internal rewrite
RewriteRule ^sub/channel-([^/]*)-(.*)\.html$ /sub/index.php?channel=$1&data=$2 [L]
The above should change http://example.com/sub/index.php?channel=NBC&data=2012/02/12 to http://example.com/sub/channel-NBC-2012/02/12.html

.htaccess, rewrite part of url for specific domain

I would like to rewrite a part of an url for a given domain like:
www.example.com/.* -> www.example.com/sub/.*
i.e rewriting www.example.com to www.example.com/sub while keeping the rest of the path.
I have tried many approaches for several hours and this is the one most promising so far, however still unsuccessful:
RewriteEngine On
RewriteBase /
RewriteRule ^www.example.com/(.*)$ http://www.example.com/sub/$1 [R=301,L]
Any ideas what is wrong with the one above or other suggestions?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example.com
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^ http://www.example.com/sub%{REQUEST_URI} [R=301,L]
The pattern which comes after RewriteRule syntax matches the Request URI not HTTP HOST. So for example www.example.com/hello the part hello will be used in pattern to be match.
#Start with hello do your stuff
RewriteRule ^hello target_url [Flags]

How to rewrite /?custom_ref= with /?ref= using htaccess rewrite

How to rewrite
/?custom_ref=
with
/?ref=
using htaccess rewrite???
RewriteEngine On
RewriteCond %{QUERY_STRING} custom_ref=(.*)
RewriteRule (.*) /?ref=%1 [R=301,L]
Can you please tell me the directory/file that you using this query on so I make sure this code will only work with it.

modrewrite - rewrite to new domain from subdirectory

Damn you modrewrite
I have a website hosted at a url like:
http://mydomain/mocks/thesite/
Now I want to move it to a new domain
http://thesitesdomain.com/
My htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule (.*) http://www.thesitesdomain.com/$1 [R=301,L]
Now this works fine as long as there is something after /mocks/thesite/. eg: http://mydomain/mocks/thesite/index.html redirects to http://www.thesitesdomain.com/index.php.
However the problem is that:
http://mydomain/mocks/thesite/ redirects to http://thesitesdomain.com/mocks/thesite/. Any idea why? How to stop this?
The .htaccess file is in the root of /mocks/thesite/ (if that helps)
Thank you
You should try to use the variable REQUEST_URI you might have a little more success with that. It should be the request uri and file name. To
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI} [R=301,L]
I can't remember but to also redirect with the query string (get variables) I think you need to add it like this.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
Been a while since really doing a domain redirect....
BTW this is a good read on htacces configuration:
http://corz.org/serv/tricks/htaccess2.php

Resources