Redirect after rewrite not working - apache

We have example1.com redirecting to example2.com - this works (also enforces ssl).
But we have a lot of links in the form of example1.com/dir/subdirectory that need to go to example2.com/dir/differentsubdirectory.
Redirect "/skill-catalog" "/catalog"
and
RewriteRule ^/home/skill-catalog/(.*)$ /home/catalog/$1 [R,L]
I have tried each and neither works. What am I doing wrong or am I missing the order they need to be in? Also would this be easier to do in htacess file for wordpress or how I am currently doing it - in the httpd conf file?

Try the following rule:
RewriteRule ^/home/skill-catalog/(.*)$ https://example2.com/home/catalog/$1 [R,L]

Related

Redirect all except localhost

I am looking for a way to do the following redirect (in an .htaccess file):
/something to /something-else
But this should happen for all hosts except for localhost. When any script tries to access /something from localhost (the web-host) then it should not redirect.
I have looked around but have not yet seem what I need in order to do such a special case redirect.
Thanks!!
Johann
Add this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !localhost
RewriteRule ^something$ /something-else [L,R]

How can I change a localhost URL with mod_rewrite or mod_proxy?

I've got MAMP running (Apache:80 MySQL:3306), and my localhost has a number of folders containing projects:
127.0.0.1/
127.0.0.1/test1/
127.0.0.1/test2/
I've got my hosts file setup properly so jlc.dev/test1 and jlc.dev/test2 are working fine.
Where I'm having trouble is I'd like to have the URL test1.dev to run the app at /test1 and the URL test2.dev to run the app at /test2. (I'm not sure what the right term is there, but I hope I've got the point across.) Based on one of the responses in this post, it sounds like it can be achieved with mod_proxy or mod_rewrite in the .htaccess file.
What I'm assuming I do is direct test1.dev to 127.0.0.1 in my hosts file, and have an .htaccess file that looks at the referring domain and rewrites to /test1 appropriately.
Little help? Thank you!
You'll need to direct both test1.dev and test2.dev to 127.0.0.1. Then add this to the htaccess file in the document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.dev$ [NC]
RewriteCond $1::%1 !^([^/]*)/.*::\1
RewriteRule ^(.*)$ /%1/$1 [L]

Apache rewrite from root folder, force https

I would like to redirect traffic from mysite.com to mysite.com/folder, but also enforce SSL on all pages.
Currently we have this in httpd.conf we found from a sample somewhere, but I don't think that it is working correctly all of the time.
RewriteEngine on
RewriteRule ^/$ https://www.mysite.com/folder/ [R]
The ^/$ only matches if the request is for exactly /, i.e. the root of the site. Anything else is not matched by it. Try .* instead.
Note: if this rewrite rule is active for both the HTTP and the HTTPS version, that'll send you into an infinite redirect loop. In that case, you may need some kind of RewriteCond test, too.

.htaccess: Redirect depending on accessed url

I want to, in my .htaccess, redirect the user to another url depending on what the user accesses.
In this case, http://example.com/awesome.com and http://awesome.com is the same site, and if the user is accessing http://example.com/awesome.com, I want him or her to be redirected to http://awesome.com.
Is this feasible?
Edit: With the help of answers, I came up with this working solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^awesome.com$
RewriteRule ^(.*)$ http://awesome.com/$1 [R=301]
you can use mod_rewrite (apache2 module)
this is the .htaccess that i use in order to redirect from my old domain to my new one (while keeping the link strcture e.g www.domain1.com/link/linkb.html becomes www.domain1.gr/link/linkb.html)
RewriteEngine On
RewriteCond %(www\.)?domain1\.com$ [NC]
RewriteRule .* http://www.domain1.gr%{REQUEST_URI} [R=301,L]
google mod_rewrite for more information (syntax etc)
Not entirely sure about .htaccess, but you could just use server code on your 404 page to redirect them appropriately; this way you could collect stats, setup a toolbar, or whatever other actions you might want to take.
.htaccess is about authorization, not redirection. I recommend you look at the redirection support for Apache (or whatever web server you're using), which is a much better fit for this problem and just make sure your .htaccess/authorization is in line with the target.
This rule should do it:
RewriteRule ^awesome\.example(/.*)?$ http://www.awesome.example$1 [R=301,L]
Check the Redirect & RedirectMatch options in apache. For simple cases, like yours it's simplier than a mod_rewrite.
Redirect /awesome.com http://ww.awesome.com
or
Redirect permanent /awesome.com http://ww.awesome.com
Now, if example.com and awesome.com are on the same apache server and same virtualhost you're maybe mising the named bases virtualhost things and you're maybe trying to make something really more complex than a simple named base virtualhost definition.

.htaccess require SSL for a particular URL

I want to force Apache to use HTTPS for a particular URL in the following form:
https://www.example.com/signup/*
so
if someone goes to any of the following example URLs directly, Apache will forward the URL over to the HTTPS equivalent site.
e.g.
http://www.example.com/signup --> https://www.example.com/signup
http://www.example.com/signup/basic+plan --> https://www.example.com/signup/basic+plan
http://www.example.com/signup/premium --> https://www.example.com/signup/premium
Anyone know how?
Thanks in advance
Thank Murat,
Yours almost worked but figured out how to get it to exactly work.
The following is what works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Notice that I didn't include somefolder in the www.domain.com rewriterule
I think this was what i used:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
(from here)
You can use the Redirect directive:
Redirect 301 /signup https://www.example.com/signup
This will automatically preserve anything following /signup in the URL. Be sure to configure this directive only on your non-SSL site, or it might get into a recursive loop!
You should take a look at mod_rewrite documentation
I used the following to require the checkout section of a website to require SSL:
<Directory "/var/www/html">
RewriteEngine on
Options +FollowSymLinks
Order allow,deny
Allow from all
RewriteCond %{SERVER_PORT} !^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule ^checkout(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
So for example, hitting http://www.example.com/checkout redirects to https://www.example.com/checkout
The rule will skip file extensions that are typically included within a page so that you don't get mixed content warnings. You should add to this list as necessary.
If you want multiple pages change the RewriteRule to something like:
RewriteRule ^(checkout|login)(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Of course, the directory should match the actual path on your server. This page may also help with some more information for your specific needs: http://www.whoopis.com/howtos/apache-rewrite.html
I'm using this on a website that runs Plesk 8.6 but that shouldn't matter. This is in my vhost.conf file which is like putting it in your httpd.conf file. I'm not sure if you'd need to adjust anything to use it in a .htaccess file but I doubt it. If adding to a conf file don't forget to restart apache to reload the configuration.
If you are like me and want to use SSL only on particular pages then you also want a rewrite rule that sends you back to regular http for the rest. You can use the following for the reverse effect:
RewriteCond %{SERVER_PORT} ^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule !^(checkout|login)(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R]
If you are using Plesk like I am keep in mind that all non-SSL traffic uses the vhost.conf file but all SSL traffic uses the vhost_ssl.conf file. That means your first rewrite rule to require SSL would go in the vhost.conf file but the second rule to force back to non-SSL will have to go in the vhost_ssl file. If you are using httpd.conf or .htaccess I think you can put them both in the same place.
I've also posted this tutorial on my blog: Apache rewrite rules to force secure/non-secure pages.
You can do this with mod_rewrite -
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/signup https://example.com/signup
RewriteRule ^/signup/(.*)$ https://example.com/signup/$1
Should work, though I haven't tested it.
-- edit --
Correction, I just tried this on one of my servers, and it works fine for me. You may want to doublecheck your mod_rewrite configuration. Also, if you're using .htaccess, you'll want to make sure overrides are allowed for that directory.
As a side note, this assumes your SSL traffic is coming over port 443. If it isn't, you'll need to adjust the rewrite condition accordingly.
.htaccess files are normally placed in a scope with Options -FollowSymLinks, which blocks Rewrite rules. This is often a security rule.
So a more trivial thing is often needed like this one:
<If "%{HTTPS} != 'on'">
Redirect 301 /your/path https://www.example.com/your/path
</If>
This is a small enhancement to the answer of Greg Hewgill.