RewriteRule help directing to and from http/https - mod-rewrite

In my Apache config file, I have the following to redirect to https if the page name has login in it:
<VirtualHost 1.2.3.4:80>
-- snip --
RewriteEngine On
RewriteRule ^/(.*)login(.*) https://domain/$login$2 [L]
</VirtualHost>
And here is the part that I am having troubles with. I want to redirect every page that does not match ^/(.*)login(.*) back to http.
<VirtualHost 1.2.3.4:443>
-- snip --
RewriteEngine On
RewriteRule ^/(.*) http://domain/$l [L]
</VirtualHost>
But as I have it, I created a circular rewrite.
What am I missing?
p.s., the IP 1.2.3.4 is bogus. :)

Not tested, but should work with:
RewriteEngine On
RewriteRule ^(.*)login(.*) https://domain/${REQUEST_URI} [L]
RewriteRule (.*[^l][^o][^g][^i][^n].*) http://domain/${REQUEST_URI} [L]
Maybe you can also forget the matching of (.*[^l][^o][^g][^i][^n].*) qnd replace by .* since the [L] should stop the HTTPS request from being match after their rewite rule. To be tested.

This solved my issue:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(.*)login(.*)
RewriteRule ^/(.*) http://domain/$1 [L]

Related

Rewriterule not applied in Apache 2.4 httpd.conf

I would like to redirect via a RewriteRule (mod_rewrite) enabled in httpd.conf my URL:
https://mysite.domain.tld/index_php_file.php?ab=ident_keys&ac=5GU7VBNAH45DA5
TO:
https://mysite.domain.tld/index_php_file.php?ab=ident_key_1024&ac=5GU7VBNAH45DA5
I have tried it with a number of rules without luck:
RewriteCond %{HTTP_HOST} hmysite.domain.tld
RewriteRule ^/index_php_file\.php\?ab=ident_keys&ac=$ https://hmysite.domain.tld/index_php_file.php?ab=ident_key_1024&ac= [R=301,L,QSA]
nor
RewriteCond %{QUERY_STRING} ^ac=(.*)$
RewriteRule ^/?([a-z-0-9-_.]+)$ /$1/index_php_file.php?ab=ident_key_1024&ac=%1 [L,R=301]
seems to rewrite the URL.
Any suggestions on what I'm missing?
Thank you very much.
I found the solution, it may help someone.
RewriteCond %{QUERY_STRING} ac=([A-Z0-9]+)
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index_php_file.php?ab=ident_key_1024&ac=$1 [R=301,L]
It is enough to look for the string (containing UPPERCASE chars and numbers only) with the RewriteCond and rewrite the URL RewriteRule to the desired format and append the value of the variable from the query.
Do not forget to enable the mod_rewrite module within Apache. To take effect a restart is also necessary of course.

Apache - Rewrite url without loop

Well I say what's said in the title again, I can't find a way to rewrite all my urls without getting a loop.
I've tried many options but I can't find about one way which avoid a redirection loop:
RewriteRule ^/(.+) http://example.com/example/index.php$1 [R,L]
RewriteRule ^/?(.*) /var/www/example/index.php$1 [R]
RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/example/index.php$1 [R]
RewriteRule ^$ /example/ [L]
And below is my directory apache-conf
<Directory /var/www/example>
Options FollowSymLinks
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>
I understand why it loops though I can't imagine (either find!) the good rule to do what I want.
EDIT
Basically I'm redirecting example.fr hosted at OVH to an IP virtual machine. Does this could get involved in my issue ?
Cheers
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/example/
RewriteRule (.*) /example/index.php/$1 [L,R]
This will redirect anything that is outside /example/ to /example/index.php adding the original path in the end
EDIT:
So, if you want everything to be redirected to /project_name/index.php instead, you need to swap the word "example" both in RewriteCond and in RewriteRule lines with your project name...
PS:
The RewriteCond line here is needed to ensure the rewriting does not loop (hence the url stayed /example/contact when you tried this).
This rule should not cause a loop
RewriteRule ^/(.+) http://example.com/example/index.php$1 [R,L]
This rule will not do anything at all
RewriteRule ^/?(.*) /var/www/example/index.php$1 [R]
For this rule you should add a RewriteCond
RewriteCond %{REQUEST_URI} !^/example/
RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/example/index.php$1 [R]
This is fine
RewriteRule ^$ /example/ [L]

mod_rewrite redirect folder to URL

I've been trying for the last few hours to do something that seems simple..
http://www.mydomain.com/u/username
redirect that to
http://www.mydomain.com/goto.php?u=username
In apache or IIS?
in apache you could use an .htaccess with (in the u folder)
RewriteEngine on
RewriteRule ^([^/]*)/([^/]*)/$ goto.php?u=$2 [L]
If on IIS and you have the latest version, you can import .htaccess into it quite easily with a tool that comes with it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^u/(.*)$ goto.php?u=$1 [L]
</IfModule>
Edit: To redirect using a HTTP 302/301 response use this:
RewriteRule ^u/(.*)$ goto.php?u=$1 [R=301,L]
or
RewriteRule ^u/(.*)$ goto.php?u=$1 [R,L]

mod_rewrite one url string

Can somebody tell me how to get mod_rewrite to rename this:
our-work-section.php?id=3&title=something
to
our-work-section/something/3
Right now my .htaccess is in directory C:\workspace\www\brown, My vhost is setup for http://workspace/, So I did a RewriteBase below. I currently Have:
RewriteEngine On
RewriteBase /www/brown/
RewriteRule ^/our-work-section/?$ our-work-section.php?id=$1 [NC,L]
My error log isn't saying anything, and the page doesn't do anything. I've tried toggling slashes / here and there.
Try this one
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(our-work-section)/?$ /our-work-section.php?id=$1 [L]
EDIT
This one will work. I tested it
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(our-work-section)/([^/\.]+)/?$ /our-work-section.php?id=$1 [L]

What is wrong with this mod_rewrite redirect?

I have a simple redirect which I just can't get to work and I don't know what's wrong. The server's throwing me a 500 Internal Server Error for no reason I can understand.
I'm trying to achieve the following: if a user types the address www.example.com, it will actually be routed to domain/ subdirectory in my server. So if user requests www.example.com/index.htm, it would fetch the file from /var/www/html/domain/index.htm.
Here's what I have so far:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^(.*)$ domain/$1 [L]
Mod_rewrite is enabled and functional, as this does work:
RewriteRule ^(.*)$ domain/index.php?$1 [L]
What am I missing here?
You have to exclude the destination you want to redirect to:
RewriteCond %{SERVER_NAME} =www.example.com
RewriteCond $1 !^domain/
RewriteRule ^(.*)$ domain/$1 [L]
Otherwise you will get an infinite recursion since domain/… is also matched by .*.

Resources