1

I'm trying to write an htaccess file and I'm having trouble getting it to work properly. There's a directory on my site called 'gpio'. The htaccess file that I'm writing is in /var/www/gpio.

When someone requests the URL http://domain.com/gpio, I want the request to be redirected to a script, with 'gpio' appended to the query string. This is what I've got in my htaccess file:

# .htaccess
#DirectoryIndex index.html
RewriteEngine on

# I changed domain name a few months ago...
RewriteCond %{HTTP_HOST} ^olddomain.com/*$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

# handle the case where the directory exists
RewriteCond %{REQUEST_FILENAME} -d
# this is the part that doesn't work
RewriteRule ^(.*)$ /cgi-bin/pyindex.cgi?q=$1 [L,QSA]

# handle the case where a file is requested
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/pyindex.cgi?q=$1 [L,QSA]

The problem is that when I visit this URL, I get redirected to the script, but the requested path doesn't get appended to the query string. In pyindex.cgi, when the query string is printed, it just contains 'q='. The end result is that I'm seeing my home page when I should be seeing a page representing a category of posts.

Any thoughts on what I'm doing wrong would be appreciated.

1 Answer 1

1

Either have that rule inside /var/www/.htaccess or modify it as

RewriteRule ^$ /cgi-bin/pyindex.cgi?q=gpio [L,QSA]
RewriteRule ^(.+)/?$ /cgi-bin/pyindex.cgi?q=gpio/$1 [L,QSA]

And, the following rule tests if the request is not a file (and hence would match directories as well)

RewriteCond %{REQUEST_FILENAME} !-f  # remove ! to match files
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ravi. I put the code in /var/www/.htaccess and that solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.