.htaccess change /es/ to .es - .htaccess

I am working with a domain like:
http://www.domain.com/es/blabla.html
And I want to change the /es part for .es and convert the URLS to something like:
http://www.domain.com.es/blabla.html
I've been trying lots of possibilities but I haven't reached a solution.
For example one of them has been:
RewriteRule ^es/(.*)$ http://www.domain.com.es/$1 [L]
But it has entered in an infinite loop. Which I've tried to avoid adding this lines:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
But the server doesn't allow this calls. So I still have the problem.

Check this.
RewriteEngine On
RewriteRule ^\.html$ /es/blabla.html [L]

Related

htaccess extract path only from THE_REQUEST into a variable

This should be rather simple, but after trying for several hours and also searching everywhere, all the related answers do not suffice.
I have this so far:
RewriteCond %{THE_REQUEST} /([a-zA-Z0-9-_/\.]+)?
RewriteRule ^(.*)$ - [E=TRGT:%1]
What the match should be doing is the following:
for URL example.com it should contain nothing, or not be defined
for URL example.com/ it should contain nothing, or not be defined
for URL example.com/some-word it should contain some-word
for URL example.com/some-word/ it should contain some-word
for URL example.com/some-word/?foo=bar it should contain some-word
for URL example.com/another_word/ it should contain another_word
for URL example.com/folder/file.ext it should contain folder/file.ext
for URL example.com/some/other.dot?bar=foo it should contain some/other.dot
for URL example.com/thing.ext/?foo=bar&bar=foo it should contain thing.ext
What I have so far seems to be working, except for when the request ends in some/ -or some/?thing=wat .. then it contains some/
I think I'm missing something really simple; any help will be appreciated, thank you.
UPDATE
I've managed to achieve these exact requirements with the following code, but, after trying many ways to do it in a 1-liner it fails horribly, so I did it in several lines:
RewriteCond %{THE_REQUEST} (?<=\s)(.*?)(?=\s)
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
RewriteCond %{ENV:HREFPATH} (^.*)?\?
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
RewriteCond %{ENV:HREFPATH} /(.*)
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
RewriteCond %{ENV:HREFPATH} (.*)/$
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
If anybody can reduce that to a s3xy 1(or 2)-liner I will choose your answer; thanks in advance.
Based on the accepted answer here the following fulfills the requirements of the question:
RewriteCond %{THE_REQUEST} \s/+([^?]*?)/*[\s?]
RewriteRule ^ - [E=HREFPATH:%1]

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

How to get Apache's mod_rewrite working?

I know it's been asked a hundred times, and I also took my time searching and trying. Somehow I still can't get it to work, only partially.
I have structure of urls like these: (xxxx can be numbers as well)
index.php?mode=xxxxx
index.php?category=xxx
index.php?product=xxxx
index.php?article=xxxxx
index.php?blog&post=xxxxx
The url I'd like to see in the end of all variations:
www.example.com/something
www.example.com/something/something (in the blog&post case)
I also would like to hide index.php at the end.
If only have numbers and nothing at the end, the rules would be:
RewriteEngine on
RewriteRule ^mode/([0-9]+)$ /index.php?mode=$1 [L]
RewriteRule ^category/([0-9]+)$ /index.php?category=$1 [L]
RewriteRule ^product/([0-9]+)$ /index.php?product=$1 [L]
RewriteRule ^article/([0-9]+)$ /index.php?article=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)$ /blog&post=$1 [L]

Display particular (dynamicaly) URL without parent folder. Leave The Others Alone

I started learning rewrites today
First of all here are examples of my links
Index.php?action=pictures
Index.php?action=pictures&type=kitchen
Index.php?action=pictures&type=ceiling
Index.php?action=services
Index.php?action=services&type=shower
Index.php?action=services&type=windows
In my .htaccess file I have this
RewriteEngine On
RewriteBase /
#Do not Rewrite files or folders
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
#Ordinary
RewriteRule ^\.htaccess$ .htaccess [F]
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(.*)/?$ Index.php?action=$1 [L]
Rewrites URLs to
localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling
localhost/services
localhost/services/shower
localhost/services/windows
//etc
I like to have ALL my links working as is with an exception of services where I don't need parent folder /services/.
Result:
localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling
localhost/services
localhost/shower
localhost/window
I tried to rewrite .htaccess but either I get only parentfolder to work or only subfolder.
I tried to add this but I do understand that this matches everything...
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]
I can however hardcode it like this
RewriteRule ^window/?$ Index.php?action=services&type=window [L]
Would like to have something dynamical. If folder services -> show no folder yet still be able to see localhost/services!
Is it possible?
Think of it this way: How would Apache know if a particular string is an action, or a type of service?
Well, you have three options:
We hardcode the types of services. Anything that does not match a type must be an action.
We hardcode the actions. Anything that does not match an action must thus be a type of service.
Apache has no way of knowing: We feed it to a script, who might be able to do some magic to find out what this string is.
In your case hardcoding the actions seems like the best idea, at least when the actions are static.
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(pictures|services)/?$ Index.php?action=$1 [L]
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]

htaccess mod rewrite $_GET to variables with slashes

I would like to rewrite URL's with htaccess to better readable URL's and use the $_GET variable in PHP
I sometimes make use of a subdomain so it has to work with and without. Also are the variables not necessary in the url. I take a maximum of 3 variables in the URL
the URL sub.mydomain.com/page/a/1/b/2/c/3 should lead to sub.mydomain.com/page.php?a=1&b=2&c=3 and the url sub.mydomain.com/a/1/b/2/c/3 should lead to sub.mydomain.com/index.php?a=1&b=2&c=3 where $_GET['a'] = 1
I came up with this after searching and trying a lot
RewriteEngine on
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6&$7=$8 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5&$6=$7 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)$ $1.domain.com/$2.php [L,QSA,NC]
but what I get is an not found server error
I'm not that good at this so maybe I oversee something.
Also I would like it to work with and without a slash at the end
Should I make use of RewriteCond and/or set some options?
Thanks in advance.
When using RewriteRule, you don't include the domain name in the line. Also, make sure you turn on the RewriteEngine first. Like this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4&$5=$6
The first line will rewrite sub.mydomain.com/a/1 to sub.mydomain.com/page.php?a=1, the second rewrites sub.mydomain.com/a/1/b/2 to sub.mydomain.com/page.php?a=1&b=2, and so on.

Resources