Using two RewriteRule in .htaccess - .htaccess

I have two RewriteRule in my .htaccess file which is hosted at godaddy.First one is for redirecting http to https and works fine.Second one is for using slash instead of question marks in URL but not working.Where am I wrong?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sosyosapien.com/$1 [R,L]
RewriteBase /
RewriteRule ^low/$ https://www.sosyosapien.com/index.php?postt=$1&konu=$2&kategori=$3 [QSA,NC,L]
Edit:To be more clear,my URL now looks like :
sosyosapien.com/index.php?postt=23&konu=what_is_sosyosapien&kategori=science
But I want it to look like
sosyosapien.com/index.php/23/what_is_sosyosapien/science
But my code only does http to https redirect and cant change the URL in the way I want.Sorry I dont have enough knowledge of .htaccess' working principle.Can you please give me a code to do what I need?

You most likely do not want to add this RewriteRule to your .htaccess. It is incorrect and it is missing capturing groups that it has been referred to:
RewriteBase /
RewriteRule ^low/$ https://www.sosyosapien.com/index.php?postt=$1&konu=$2&kategori=$3 [QSA,NC,L]
If you really have to keep this, you might find out what $1, $2 and $3 are that you might want to redirect. Then, you could change this ^low/$ based on that.
Maybe, just add this, and it might work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sosyosapien\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sosyosapien.com/$1 [R,L]
RewriteCond %{QUERY_STRING} postt=(.*)
RewriteRule ^(.*)\/index\.php\?postt=([0-9]+)&konu=(\w+)&kategori=(\w+)$ ^$1\/$2\/$3\/$4$ [R,L,302]
</IfModule>
This RegEx might help you to simplify your expression, as you wish, since I'm not sure what types of conditions you might have:
^(.*)\/index\.php\?postt=([0-9]+)&konu=(\w+)&kategori=(\w+)$
Graph
This graph shows how the RegEx works:
You might need to restart apache:
sudo apachectl restart
You also want to clear your browser cache, every time that you would make a change in .htaccess.

Related

Root redirects to just one subdomain in .htaccess

I want the root of my website (www.bitumephotofest.it) to redirect to a subdomain (2016.bitumephotofest.it) (I am running a Wordpress multisite). It works.
But I have another subdomain (2015.bitumephotofest.it) and it also redirects to 2016.bitumephotofest.it.
I want the redirect to work only between www.bitumephotofest.it and 2016.bitumephotofest.it. 2015.bitumephotofest.it should be independent as it is a different website.
I tried to look for questions by people with a similar situation but there is always something different and, anyway, those solutions does not work for me.
Here is my code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 2016.bitumephotofest.it
RewriteCond %{REQUEST_URI} !wordpress/
RewriteRule ^(.*)$ /wordpress/$1 [L]
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteRule !^(2016) http://2016.bitumephotofest.it [L,R]
Does anyone know what I am missing?
Thank you in advance!
If you want the domain 2015.bitumephotofest.it stay unmodified, you can sort of exit the rewrite rule chain with
RewriteCond %{HTTP_HOST} ^2015\.bitumephotofest\.it$
RewriteRule ^ - [L]
- as the target means don't rewrite, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

.htaccess rewriting GET

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

RewriteRule in .htaccess and Reverse Proxy

Here is my problem, I`m struggling with it for a few days.
We`ve got a domain firstpart.maindomanin.com nad subdomain secondpart.maindomain.com.
Under first domain there is a first part of the project (based on SaaS commerce) and second part (under secondpart.maindomain.com) - based on Symfony. Those two parts are connected through SOAP services etc.
For firstpart.maindoman.com we are using Cloudflare.
We`ve got reverse proxy so:
firstpart.maindomain.com/uk/made is pointed to secondpart.maindomain.com/uk
and now (we cant enable cloudflare secondpart.maindomain.com due to some unrelated issues) we want to redirect all url-s from secondpart.maindomain.com/uk to firstpart.maindomain.com/uk/made
so for example
secondpart.maindomain.com/uk/furniture to firstpart.maindomain.com/uk/made/furniture
secondpart.maindomain.com/uk/sales to firstpart.maindomain.com/uk/made/sales
etc.
so we need to change domain and add 'made' between language code and rest of url
Other than that we need to redirect all urls like
firstpart.maindomain.com/uk/furniture to firstpart.maindomain.com/uk/made/furniture
(add 'made' between language code and rest of url)
and we need to do it in htaccess under subdomain secondpart.maindomain.com.
I came up with something with:
RewriteCond %{HTTP_HOST} secondpart.maindomain.com$ [NC]
RewriteRule ^([a-z]{2,3})(.*)$ http://firstpart.maindomain.com/$1/made$2 [R=301,L]
and for url like
secondpart.maindomain.com/uk/furniture
I`m getting redirection to
http://firstpart.maindomain.com/uk/made/furniture
which is fine but after redirection there is infinite loop (so except changing urls is not working)
As it turned out HTTP_HOST for both firstpart.maindomain.com/uk/made and secondpart.maindomain.com/uk is the same and it is secondpart.maindomain.com so condition is not working.
I came up also with condition like
RewriteCond %{REQUEST_URI} !^made [NC]
RewriteRule ^([a-z]{2,3})(.*)$ http://firstpart.maindomain.com/$1/made$2 [R=301,L]
so condition is not met if there is a word 'made' inside URI and in this case it is the same as in first rule.
I tried several different configurations but nothing is working.
When i tested it with http://htaccess.madewithlove.be/ everything was fine and there was not redirection.
So im assuming there is something with reverse proxy on cloudflare.
Im not an expert in htaccess but really i tried a lot of solutions and nothing is working.
I would really appreciate some help with it.
P.S. Just in case here is a .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^([a-z]{2,3})(.*)$ http://vendauat.lauraashley.com/$1/made$2 [R=301,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
This condition:
RewriteCond %{REQUEST_URI} !^made [NC]
will always be met because your regex pattern says: if the request never starts with made, but the %{REQUEST_URI} variable always starts with /. Maybe what you want is this instead:
RewriteCond %{REQUEST_URI} !^/[^/]+/made/

RewriteRule [L] still changing the url

I found myself with this problem, which is driving me a little bit crazy. I use apache's mod_rewrite for pretty URLs and I need to use dynamic subdomains in the site. Everything is great and all the server has de wildcards. I use the next code on my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1 [L]
The only problem is, even if I use the [L] flag the url of the site change to http://mysite.com/subdomain. What i want is the url to be like http://subdomain.mysite.com
The link mysite.com/subdomain is a dynamic url and is solved with another rule with the following code:
RewriteRule ^([A-Za-z]+)$ filter.php?type=subdomain&subdomain=$1
Any help would be appreciated
If you specify an external URL (which changing the subdomain does), a header redirect will take place. I don't think you can prevent that. But why not skip that step altogether, and use the second RewriteRule straight away?
I can't test this right now, but something like
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ filter.php?type=subdomain&subdomain=$1
should work.

301 Redirect with .htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^category?$
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ http://domain.com/$3/ [L,R=permanent]
Currently I have the following redirection and it is working like a charm. Now I want to make sure that the link does not begins with /category/ therefore I have inserted the condition. Unfortunately it does not seems to work. Please help. Thanks.
Another question is, how to make that the end permalink that is between the slash is selected to be redirected only. For example, I may have links like http://domain.com/downloads/26-fine-wallpapers/ and http://domain.com/downloads/icons/35-nice-icons/ and I want links like these to be redirected to http://newdomain.com/35-nice-icons/ and http://newdomain.com/26-fine-wallpapers/
I am using wordpress actually.
According to your description you only have two path segments. So your pattern should be:
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
And to exclude /category/…, you can either check the request URI path in REQUEST_URI:
RewriteCond %{REQUEST_URI} !^/category/
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
Or you check the matched value of the first group:
RewriteCond $1 !=category
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
I think you just need a prefixing /:
RewriteCond %{REQUEST_URI} !^/category?$

Resources