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]
Related
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.
I am trying to make a link that looks like https://www.exapmle.com/profile.php?u=8 to look like https://www.exapmle.com/profile/8
I have a tried variations of this in htaccess:
RewriteRule ^/profile/([0-9]+)/?$ profile.php?u=$1
RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=$1
I don't know what i am doing wrong, the links don't change and I'm not getting any errors either
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^/?profile/(\d+)(?:\.html)?/?$ profile.php?u=$1 [L,QSA,NC]
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/
I have a domain (let's say www.example.com) and would like this to point to /Example_folder/ of my server (within /var/www/).
So, if I try to goto www.example.com/images/test.html or example.com/images/test.html, it should be actually pointing at /Example_folder/images/test.html.
I tried to get this working using following code, but I can't figure out.
Trial#1:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/ [L]
If I use above code, I get ERR_TOO_MANY_REDIRECTS.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/index.html [L]
If I use above code (where index.html is specified), it would redirect but I can't get my domain to point at its subdirectories. (www.example.com/images/test.html would also point at www.example.com/index.html)
I got it working using the code from link below:
htaccess Silent Redirect to Subdirectory: Subdirectory showing when no trailing '/'
Last thing that remains is that when I point to www.example.com/Example_Folder, I want the address bar to show www.example.com, but I have not figure that out yet.
You can use this negative lookahead based rule to avoid rewrite loop:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
Rewriterule ^((?!Example_Folder/).+)$ /Example_Folder/$1 [L,NC]
Which means rewrite only if URI doesn't already start with Example_Folder/.
I want to change alle dynamic url's to static ones,
but after rewriting the dynamic url's are still responding/available.
What did I do =>
I found this Tool for SEO:
http://www.webconfs.com/url-rewriting-tool.php
I entered this:
.../filmdetails.html?var=ich_einfach_unverbesserlich_ii
Then I put into my .htaccess this:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1
Works, but now I got a problem: This URL is still available and should not be: .../filmdetails.html?var=ich_einfach_unverbesserlich_ii
How do I get rid of the dynamic url's?
Your rule only rewrites the nicer looking URL to the one with a query string. Rules only work from a "pattern" -> "target" way, the mapping won't magically work the other way. You'll have to create a separate rule in order to redirect the browser:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /filmdetails\.html\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /filmdetails/%2/?%3 [L,R=301]
Try:
Options +FollowSymLinks
RewriteEngine On
#set this to path to your filmdetails.html file (from the document root)
RewriteBase /
#checking if redirect already happened
RewriteCond %{ENV:REDIRECT_PASSED} !^$
RewriteRule $ - [L]
#Your rewrite rule
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1 [L,E=PASSED:1]
#redirecting from filmdetails.html with query string ?var=something
RewriteCond %{QUERY_STRING} ^var=(.+)$ [NC]
RewriteRule ^filmdetails.html$ filmdetails/%1/? [R]
filmdetails.html?var=something will be redirected to filmdetails/something