mod-rewrite to remove a folder - apache

I have the following URL
http://www.example.com/folder1/folder2/file.php
I want to redirect it to
http://www.example.com/demo/folder1/file.php
I tried this, but it does not work:
RewriteCond %{HTTP_HOST} www.example.com
RewriteCond %{REQUEST_URI} ^folder
RewriteRule /demo/folder(.*) /folder1/folder2/$1

Your rule is backwards - it works the other way around.
You can try with the following:
RewriteRule ^folder1/folder2/(.*)$ /demo/folder1/$1 [R,L]
Or perhaps you'd like to make folder1 dynamic:
RewriteRule ^([^/]+)/folder2/(.*)$ /demo/$1/$2 [R,L]
If one of these works for you, and you would like to make the redirect permanent, you can change [R,L] to [R=301,L].
Note: You do not need to first two lines (RewriteCond), they are unnecessary.

Related

What is the correct syntax for "if host is not foo, redirect to bar" in a .htaccess file?

This website has a ton of extra domains (note: these are not subdomains; one of them, for instance, is http://eduard.fi) that the owner (or the SEO people, rather) wants to redirect to the main domain. Instead of listing them one by one, this is what I tried:
RewriteCond %{HTTPS_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301,L]
However this creates a redirect loop. Why is that? This does not produce a server error, so for that part the syntax is correct, but it does not do what I want.
You were close, but made a logical mistake. Take a look at this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301]
An alternative would be that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^ https://masetti.fi%{REQUEST_URI} [R=301]
The RewriteCond has been slightly altered: It is the variable %{HTTP_HOST} you want to check, not %{HTTPS_HOST}which does not exist.
PS: it is a good idea to start out with a 302 redirection and only change that to a 301 once everything works as intended. That prevents issues with client side caching.

Allowing dot character in RewriteRule .htaccess

I want to allow dot in this rewrite rule:
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ index.php?D1=$1&D2=$2&D3=$3 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ index.php?D1=$1&D2=$2 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9\-]+)/?$ index.php?D1=$1 [NC,QSA,L]
If I use this one:
RewriteRule ^([a-zA-Z0-9\-\.]+)/?$ index.php?D1=$1 [NC,QSA,L]
The value of D1 in the root directory is "index.php" instead of an empty value.
Any ideas about this?
Thanks
Its maybe not obvious, but if you think about it you will get it. The Point is you already have an internal rewrite rule in place.
So if you open your website example.com you actually get example.com/index.php.
Now you add some custom rewrite rule. The First one have no dot include so index.php do not match, but the second one have a dot include so ([a-zA-Z0-9\-\.]+) will match also index.php and therefore will rewrite it to index.php?D1=index.php.
How to fix it?
Very simple: exclude index.php like so:
RewriteCond %{REQUEST_URI} !^/?index\.php
RewriteRule ^([a-zA-Z0-9.-]+)/?$ index.php?D1=$1 [NC,QSA,L]

Where is my mod rewrite fail?

I have got url
ipaddr/opensys/base/
and some other similar, like
ipaddr/opensys/base/something.php?olol=yep
I want to remove from url "opensys" and display there "center", i want to see this links working:
ipaddr/center/base/
and
ipaddr/center/base/something.php?olol=yep
I did it with symlinks, but it was not good, because system is very difficult and with symlink some plugins not works, I want to do it with .htaccess only, after it all will be ok.
My htaccess is:
but links, which i want to see is not working, why?
RewriteEngine on
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [L]
RedirectMatch 301 ^/opensys/base/(.*)$ /center/base/$1
Redirect works good, but i see 404, rewrite rules is not working. Why?
You also need to handle the /center/base/ now in the .htaccess file as you must be handling opensys earlier. Something like this -
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [R=301]
RewriteRule ^/center/base/(.*)$ /index.php?args=$1 [QSA,L]
You can use this code in root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /opensys/(base/\S*)\s [NC]
RewriteRule ^ /center/%1 [R=301,L,NE]
RewriteRule ^center/(base/.*)$ opensys/$1 [L,NC]

htaccess RewriteRule for folder

I have a bunch of rewrite rules I would like to implement. I would like to redirect anything that has /blog/tag/... to my root url.
For example, all of these:
blog/tag/button-sets/
blog/tag/icons/
blog/tag/order-now/
blog/tag/body-attributes/
Would simply route to: www.url.com
I can do it on a case-by-case basis like below, but would like to redirect a bunch with 1 rule. Any help would be greatly appreciated
RewriteCond %{HTTP_HOST} ^url\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.url\.com$
RewriteRule ^blog\/tag\/button\-sets\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]
Why dont you just remove the button-sets part of your rule like so
RewriteRule ^blog\/tag\/?(.*)$ "http\:\/\/www\.url\.com\/" [R=301,L]
Havent tested it but should be OK?
In stead make it such that it captures everything after the second / after blog/tag/(ANYTHING)/(CAPTURE) which will redirect to www.url.com/CAPTURE
RewriteRule ^blog\/tag\/.*\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]

how do I redirect from one page to another with mod_rewrite?

All the advice online says do:
rewrite 301 URL-A URL-B
But that won't work if I turn on mod_rewrite (it seems?) with RewriteEngine on
So, I'm bad a regex, but shouldn't need it here. How do I do:
RewriteCond %{HTTP_HOST} ^untamed-adventures.com/travel/How/tabid/58/Default.aspx [NC]
RewriteRule ^(.*)$ http://untamed-adventures.com/ [R=301,L]
%{HTTP_HOST} expands to the host of the request, so it could never match untamed-adventures.com/travel/How/tabid/58/Default.aspx, only untamed-adventures.com.
If you want to forward http://untamed-adventures.com/travel/How/tabid/58/Default.aspx to http://untamed-adventures.com/, try this:
RewriteCond %{HTTP_HOST} =untamed-adventures.com
RewriteRule ^/travel/How/tabid/58/Default.aspx$ http://untamed-adventures.com/ [R=301]
The L flag is redundant; a forward is always final.
Not at all clear what you're trying to do. HTTP_HOST is the hostname part in the requested URL, in this case, "untamed-adventures.com", so that RewriteCond will never match.
I think that what you're trying to do is:
Redirect 301 /travel/How/tabid/58/Default.aspx http://untamed-adventures.com/
In which case, mod_rewrite isn't needed at all.