Sparkmasterflex
Sparkmasterflex

Reputation: 1847

.htaccess behaving differently on server

So I have a website that successfully rewrites the url parameters to a clean url but on another server the same .htaccess does not work.

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
  RewriteRule .* – [F,L]

  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteCond %{SCRIPT_FILENAME} !-f

  RewriteRule ^project/(\d+)*$ ./project.php?project=$1
</IfModule>

What is not working is the last line there. I need something.com/project/star-trek to be the url the user gets when clicking a link, but they are served up something.com/project.php?project=star-trek while still seeing the clean url.

On Server1 this code above works perfectly but the same code on Server2 does not. RewriteRule does work on Server2 because we can use

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This does work to remove the '.php' on Server2 as well as Server1.

I did check that .htaccess is being allowed in the httpd.conf and I have changed all the AllowOverwrite(s) to All

I'm at a loss here and would appreciate any help. Thank you

Upvotes: 0

Views: 195

Answers (1)

CD001
CD001

Reputation: 8472

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
    RewriteRule .* – [F,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^project/(.*?)/?$ ./project.php?project=$1 [L]
</IfModule>

That works on my machine, in your original script the bits I noticed were:

  • the RewriteEngine wasn't switched on (but it may have been in httpd.conf or httpd-vhosts.conf)
  • the RewriteRule was only matching digits and not allowing trailing slashes: ^project/(\d+)*$

I've made the RewriteRule an ungreedy wildcard match that excludes trailing slashes; works in an .htaccess file under Apache 2.4.

Upvotes: 1

Related Questions