Mastergalen
Mastergalen

Reputation: 4389

.htaccess Redirect 404 Error

I've been trying to get this right for 1 or 2 hours now, searching for similar problems and trying the solutions. This is probably is one of those thing again where the answer is really obvious.

I am trying to redirect everyone who is accessing domain.com/title/whatever and domain.com/title/anotherpage to a PHP script, but it is giving me a 404 error when I try to access the page.

I am using this code in my htaccess file:

RewriteEngine on
RewriteRule ^/plugins/(.*)$ /yourscript.php?plugin=$1 [L]

Here is a full copy of my .htaccess file.

ErrorDocument 404 /error/404.php

#Prevent full listing of files
Options -Indexes 

#Redirect plugins
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/plugins/(.*)$ /foobar.php?plugin=$1 [NC,L]
</IfModule>

#Hide .php extension
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
</IfModule>

#compress
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript 

I've tried placing the foobar.php script in both the root folder and in a folder name plugins.

Thank you in advance for any help.

Upvotes: 1

Views: 1182

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

It looks like everything's in the right place, but you might want to try removing the leading slash in your regular expression (or at least make it optional):

RewriteRule ^/?plugins/(.*)$ /yourscript.php?plugin=$1 [L]

In an htaccess file, apache removes the prefix (leading slash) of the URI before it hands it off to the rules in an htaccess file.


domain.com/plugins/whatever gets redirected correctly. However, domain.com/plugins also gets directed to the script. Instead, I would like to have it so that it directs to a index.php file in a folder name "plugins"

Then you need to change the above rule to:

RewriteRule ^/?plugins/(.+)$ /yourscript.php?plugin=$1 [L]

(Note the * is change to a + to match at least 1 character). You probably don't need to add any more rules as long as index.php is a valid index, meaning if you just go to /plugins/ apache will automatically server the index.php file.

Upvotes: 2

Related Questions