Apache URL redirection - apache

I would like to set up an Apache redirect rule which will accept the following browser URL:
192.168.100.128/test
and transform it into the following:
http://192.168.100.128:9001/forms/frmservlet?config=testjpi
I have had a look at the httpd.conf and have tried to use both the Alais and Rewrite modules but I can't get either to work.
Alias:
Alias /test http://192.168.100.128:9001/forms/frmservlet?config=testjpi
And also Rewrite:
<IfModule rewrite_module>
RewriteEngine on
RewriteRule http://192.168.100.128/test http://192.168.100.128:9001/forms/frmservlet?config=testjpi
RewriteRule /test http://192.168.100.128:9001/forms/frmservlet?config=testjpi
</IfModule>
Any ideas if what I'm doing is even possible, and if so, how do I go about getting it right? I have limited knowledge of Apache so I'm going in a bit blind.
TIA.

Alias won't work on URLs it only works to directories.
Try:
Redirect permanent /test http://192.168.100.128:9001/forms/frmservlet?config=testjpi
for more documentation: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirect

Related

htaccess URL rewrite is not working in production but works on local

I have a problem with URL rewritting and htaccess. I have an URL like www.example.com/index.php?fc=module&module=mymodule&controller=name&id=35 but it's too complex for people so I want to change it. I created a directory in my host like example.com/web/title-for-name/ where I put this .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?fc=module&module=mymodule&controller=name&id=35$1 [L,NC]
I've tested it in my local and it seems to work but when I test it in production it just redirect me changing my URL. Mod-rewrite is enabled because my web is rewriting other URLs. Also AllowOverride is set to All. The main difference between my local and production is that I'm not using Varnish.
I've solved this problem. There was an URL rewrite before mine that changed the domain .es to .com and changed the rest of my URL so I just changed the link I use to the .com domain.
Thanks everyone.

Apache redirect directory to index.php

I'm trying to redirect a simple folder to /folder/index.php.
My URL looks like this : site.com/folder and I want site.com/folder/index.php.
Throught de web I only found the reverse way but I do want to see my index.php so that I can later use links like site.com/folder/index.php#344
My .htaccess located in /folder/ looks like this :
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/(.+)$ index.php
</IfModule>
And of course it doesn't work.
Maybe is apache ignoring my file ? Maybe some others .htaccess are interfering ? I have no idea since I'm new to apache...
Thanks for your help
You could just skip this mod_rewrite stuff and use mod_dir:
DirectoryIndex index.php
With that in the htaccess file in /folder, going to http://site.com/folder you will get served the contents of index.php.

htaccess redirect error in domain example.com:443

I've got a problem with a htaccess redirect.
What I like to do:
I want to redirect the link https://www.example.com/folder to https://www.example.com/folder/subfolder.
So normally I would use:
redirect /folder /folder/subfolder
But I've got the problem, that my browser is redirected to http://www.example.com:443/folder/subfolder. So no https but a nice :443 at the end. And the browser gives me an 404 error.
Does anybody has an idea how I can redirect the url correctly?
Thank you.
Try this instead:
redirect permanent /folder https://www.example.com/folder/subfolder
EDIT: If mod_alias isn't working for you you can try using mod_rewrite based rule like this:
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^folder/?$ https://www.example.com/folder/subfolder [L,R=301,NC]

absolute links and mod_rewrite

I want to use mod_rewrite and I set everything up to use "absolute links" in order to overcome URL resolving issue. Now my links look like (/file.php) instead of (./file.php) and everything works ok on localhost.
Unfortunately, when I put the project on my actual server, links are not valid anymore so I want to know where "/" is pointing to and how to resolve this issue.
(I do not want to use html base tag at this point.)
localhost: http://project.local
Actual host: http://project.com
And here is what I have in my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^page/([0-9-]*) /home.php?page=$1 [QSA,L]
try using rewritebase
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^page/([0-9-]*) /home.php?page=$1 [QSA,L]

.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.