Forum Moderators: phranque

Message Too Old, No Replies

rewriterule or rewritecond not working for cgi-bin scripts

cgi-bin rewritecond rewriterule htaccess

         

davidj003

5:54 pm on Dec 18, 2014 (gmt 0)

10+ Year Member



Hi,

We want all urls that start as example888.com to redirect to www.example888.com

The following

rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

works fine for
example.com/samplepage.html (or any other combination with folder and html page)

but not for

example.com/cgi-bin/scripts/sample.cgi

Is the rewriterule or rewritecond missing something or do I need to create some other rule or condition

Thanks
David

[edited by: phranque at 11:45 pm (utc) on Dec 18, 2014]
[edit reason] exemplified domain [/edit]

phranque

11:52 pm on Dec 18, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, davidj003!


if your config has a ScriptAlias directive, it may be referring your /cgi-bin/ request to a directory that isn't in your document root directory and therefore the .htaccess doesn't get seen for that request.

lucy24

1:00 am on Dec 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As long as you're in there...

#1 an [NC] flag will not make the entire universe case-neutral. Keep it safe by using the standard casing throughout.
#2 domain-name redirects are best expressed as a negative: "If the requested hostname is anything other than my preferred form" -- including preferred casing. (19 times out of 20, a request for EXAMPLE.COM will be an unwanted robot. So you may as well fob them off with a 301 response instead of serving up the whole page.)
#3 By default, a Regular Expression begins as soon as it can and continues as long as it can. So although the ^ and $ in the pattern aren't actively harmful, they're also not needed
#4 The final flag really is wrong. [NC] doesn't belong in any situation where you're not matching alphabetic text. What you do need is the [L] flag. It's not implicit in [R].

So the optimal form of the rule would be
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Now back to the original question. Surely your cgi isn't part of a visible URL that can be requested "cold"? * If it's only invoked in internal requests, then you have a non-problem. The page that calls the script can only make requests to the correct hostname.


* Looking up at the top of the present page as I type this post, I see I'm in "postv5.cgi" or, for variety's sake, "bill-postv5.cgi". But I can't navigate here of my own volition; the URL was requested when I clicked the Preview button. By which point the hostname has already been regularized.

davidj003

7:57 pm on Dec 19, 2014 (gmt 0)

10+ Year Member



Hi,

Thank-you!

The problem is that its href links from the search engines.

Sounds like no solution.

David

phranque

8:52 pm on Dec 19, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



are you saying google has indexed these /cgi-bin/ URLs?

davidj003

9:05 pm on Dec 19, 2014 (gmt 0)

10+ Year Member



yes so we want to redirect 301

phranque

9:13 pm on Dec 19, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what does your ScriptAlias directive look like?

davidj003

8:13 pm on Dec 26, 2014 (gmt 0)

10+ Year Member



Hi,

ScriptAlias /cgi-bin/ "/var/www/username/cgi-bin/"

Thanks
David

phranque

1:06 am on Dec 27, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



is /var/www/username/cgi-bin/ a subdirectory of the path specified in your DocumentRoot directive?

lucy24

7:49 pm on Dec 27, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



david, is it your own server or a virtual setup? (Probably not important, but it's as well to cover all possibilities.)

davidj003

3:24 pm on Dec 29, 2014 (gmt 0)

10+ Year Member



Hi

Q1: /var/www/username/cgi-bin is subd of DR - No

DR is /var/www/username/htdocs

Q2: Yes its my own server. I can change httpd.conf

Thanks-you!

David j.

lucy24

5:17 pm on Dec 29, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, then the next question is: Where are the current RewriteRules located? Lying loose in the config file, or in a <Directory> section, and if so, which directory? You're not using a <Location> envelope are you?

davidj003

7:01 pm on Dec 29, 2014 (gmt 0)

10+ Year Member



Hi,

The rewrite rules are in an htaccess file under Document Root

In the <VirualHost> container I have setup

<Directory /var/www/username/htdocs>
Options -Indexes +IncludesNOEXEC
AllowOverride All
AddType text/html .htm .html
AddHandler server-parsed .htm .html
ErrorDocument 404 /404.html
</Directory>

Thanks
David j

phranque

8:55 pm on Dec 29, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



David, please reread my first response in this thread.

davidj003

9:03 pm on Dec 29, 2014 (gmt 0)

10+ Year Member



Hi,

Therefore given this configuration - it can't be done.

Thanks
David j.

phranque

9:06 pm on Dec 29, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you can add a .htaccess file with hostname canonicalization directives to the /cgi-bin/ directory.

lucy24

10:37 pm on Dec 29, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The rewrite rules are in an htaccess file under Document Root

I suppose it's no use asking why you've even got a permanent htaccess for a live site if it's your own server :( But that makes the explanation pretty clear: requests for scripts will never see the htaccess file that contains the redirects.

You may or may not be able to put the RewriteRules one level higher, in the /users/ directory. I'm inclined to think not, though some htaccess directives will work in levels between config and root. But then, I've also never quite figured out why you want your scripts to be indexed. If they're not indexed, neither URL nor location makes any particular difference.