Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

htaccess rewrite not working WordPress

  • SOLVED

Hi All,

I have a theme that uses a dynamically generated CSS file in PHP where htaccess is then used to rewrite the .css requests to .php.

The problem is that it works on my friends server but it doesn't on mine. I've asked the host if mod_rewrite is enabled and they say it is so I'm not sure what the issue is.

I can access the file when visiting it with the php extension but the .css produces nothing.

How do I debug the problem or fix it?

The htaccess file contents are as follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule custom.css custom.php [L,QSA]
</IfModule>


Many thanks.

Answers (2)

2011-04-26

Julian Lannigan answers:

You could try something like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^custom\.css$ /custom.php [R=302,L]
</IfModule>


I used the following to get the above: [[LINK href="http://stackoverflow.com/questions/1668941/how-to-use-rewriterule-to-redirect-to-a-php-file-in-the-same-folder"]]http://stackoverflow.com/questions/1668941/how-to-use-rewriterule-to-redirect-to-a-php-file-in-the-same-folder[[/LINK]]


drew comments:

This made it redirect to domain.com/custom.php and not the URL where the file was called from.

IE: http://www.domain.com/wp-content/themes/themename/css/custom.php

But I can't just hard code that in because it's needs dynamic URL path.

I hope that makes sense.


Julian Lannigan comments:

Try removing the preceding slash for the destination. Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^custom\.css$ custom.php [R=302,L]
</IfModule>


Julian Lannigan comments:

I'm sorry this will keep the relative folder, try this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)/custom\.css$
RewriteRule ^custom\.css$ %1/custom.php [R=302,L]
</IfModule>


drew comments:

Already did try that, that was my immediate thought. But that then puts the server path into the url and not the URL.

IE:

http://domain.com/var/chroot/home/content/l/a/s/path/html/path/wp-content/themes/themename/css/custom.php

+ I don't want the actual php showing up, I want that masked which I assume is done by removing the R=302 and just making it [L]


Julian Lannigan comments:

Ok this should work.

Remove the custom.css related rewrite rules.

<FilesMatch "^custom\.css$">
SetHandler application/x-httpd-php
</FilesMatch>


What that will do now is tell apache to process any file named "custom.css" as a php file. Just make sure you have the "header('Content-type: text/css');" in the css file.


drew comments:

This was the one that worked:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)/custom\.css$
RewriteRule ^custom\.css$ %1/custom.php [L]
</IfModule>

Many thanks. =)

2011-04-26

Vidyut Kale answers:

You'll need to tell the server that its css

add at the very beginning of custom.php


<?php
// We'll be outputting a CSS
header('Content-type: text/css');
?>


drew comments:

I already have this, that's not the problem. Like I said, it works perfectly on a friends server, visiting the .css link produces exactly the same as visiting .php but on my server, .css produces 404.


Vidyut Kale comments:

Can't you simply change the header or enqueue the correct file?


Vidyut Kale comments:

Generally, if it is possible to reference the correct file, its best practice. your stylesheet gets loaded with every page of the site. Why redirect?


Vidyut Kale comments:

Try
<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule (.*)/custom.css $1/custom.php [R=302,L]

</IfModule>