Jean-philippe Emond
Jean-philippe Emond

Reputation: 1614

htaccess url rewrite issues

I have an issues with my htaccess file rules..

My code redirect all url with http://website.com/secure/* to the CGI-BIN without cgi-bin folder in the url.

RewriteEngine on
RewriteRule ^secure/$ /cgi-bin/secure/home.pl
RewriteRule ^secure/?(.*)$ /cgi-bin/secure/$1

The problems is: I got a 404 not found if I use:

http://website.com/secure  (without last slash)

Also, I have try to merge this .htaccess with a http to https (only for this section and not all of entire website.

I don't know if its possible to help me with it and if you have a good tutorial to explain me the basic/complex htaccess coding.

Thanks For your time.

Upvotes: 1

Views: 52

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270775

First, redirect to HTTPS for input URL's matching /secure. There are a few ways to approach it with a RewriteCond. We'll do so by matching %{REQUEST_URI}.

All that's really missing from your RewriteRule is an optional slash in the first rule. I'll add /? to the first one and use (.+) instead of (.*) in the second one.

RewriteEngine On

# First redirect to HTTPS if HTTPS isn't already on
# The first RewriteCond makes sure this only matches for /secure
# and doesn't force everything on your site into https.
RewriteCond %{REQUEST_URI} ^secure
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,QSA]

# Then fix the rewrite rules
# Nothing after secure goes to home.pl
RewriteRule ^secure/?$ /cgi-bin/secure/home.pl [L]
# Something present after secure/ (.+) is captured in $1
RewriteRule ^secure/(.+)$ /cgi-bin/secure/$1 [L]

If the rewrites into cgi-bin/ should preserve query strings, use [L,QSA] instead of just [L]

Upvotes: 1

Related Questions