htaccess mod_rewrite HTTP 500 error - apache

This particular code works on one server, but not the other. The working one has Apache2, the other Apache1. How can I make it work on both?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# SEO translation
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[a-z]{2}/)?.*-p-([0-9]+)$ page.php?page_id=$1&%{QUERY_STRING}
RewriteRule ^(?:[a-z]{2}/)?(.*) $1?%{QUERY_STRING}
</IfModule>
Input URL alternatives:
www.domain.com/some-fancy-title-p-10
www.domain.com/en/some-fancy-title-p-10
www.domain.com/sv/some-fancy-title-p-10

Try duplicating the conditions. They only apply to the single immediately following RewriteRule so you need to duplicate them for both rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(?:[a-z]{2}/)?.*-p-([0-9]+)$ page.php?page_id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(?:[a-z]{2}/)?(.*) $1 [L,QSA]
Also make the leading slash optional (/?).

It appears ?: is not valid regexp in apache1. Avoiding it solves the problem.

Related

Use .htaccess to cease processing if particular string is captured in URL

I need the following .htaccess file to redirect requests for pages ending in njiswebhook to \webhooklanding.php.
At the moment the requests are still being passed to index.php and are being routed by Slim. How can I tell .htaccess to cease processing if a particular condition is met?
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*njiswebhook /webhooklanding.php [QSA,L]
RewriteRule ^ index.php [QSA,L]
Make an exception from last rule , otherwise nothing will pass to webhooklanding.php like this :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*njiswebhook webhooklanding.php [QSA,L]
RewriteRule !^webhooklanding\.php index.php [QSA,L]
The [END] flag stopped processing upon reaching that clause.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*njiswebhook /webhooklanding.php [QSA,L,END]
RewriteRule ^ index.php [QSA,L]
The above solution has been implemented and tested.

Apache .htaccess to redirect all URLs except for files and directories

I am developing a web site currently located at a subdomain. In the .htaccess file I have the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z_\-]+)/?$ /index.php?url=$1 [L,NC]
RewriteRule ^([a-zA-Z_\-]+)/(.+)/?$ /index.php?url=$1&$2 [L,NC]
When I try to load an existing css file e.g. http://subdomain/path/to/existing/file.css Apache still redirects it to index.php. Is it possible to redirect all URLs to index.php except for existing files and directories?
The problem with your code is that the last rule is not conditioned, as you can only have one RewriteRule per condition or set thereof.
There's more than likely a better way to do this (perhaps using the skip flag S), but you can repeat your conditions in the meantime.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z_-]+)/?$ index.php?url=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z_-]+)/(.+)/?$ index.php?url=$1&$2 [L,NC]
Also, and as you have specified the NC flag, you need not specify A-Z in the pattern. Also, no need to escape the hyphen -.
Update: You could also try setting an environment variable, per this answer:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# If the conditions above are true, then set a variable to say so
# and continue.
RewriteRule ^ - [E=TRUE:YES]
RewriteCond %{ENV:TRUE} = YES
RewriteRule ^([a-zA-Z_\-]+)/?$ /index.php?url=$1 [L,NC]
RewriteCond %{ENV:TRUE} = YES
RewriteRule ^([a-zA-Z_\-]+)/(.+)/?$ /index.php?url=$1&$2 [L,NC]

.htaccess rewrite rules issue

I am trying to rewrite some urls using mod_rewrite.
I want to rewrite
localhost/exotica/pet/somePet/
to
`localhost/exotica/index.php/types/get/somePet/`
I have managed to remove index.php by using
RewriteCond $1 !^(index\.php|resources|uploads|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /exotica/index.php/$1 [L]
so now localhost/exotica/types/get/somePet works
i have tried adding as first rule
RewriteRule ^pet/([A-Za-z0-9-]+)/?$ type/get/$1 [N]
but it simply doesnt work so please help me i have no idea how to enable it
I managed to solve it by adding a route $route['pet/(:any)'] = "type/get/$1";, but I would prefer to do it using .htacess file
Try your rules like this in reverse order:
RewriteEngine On
RewriteBase /exotica/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pet/([A-Za-z0-9-]+)/?$ types/get/$1 [L]
RewriteCond $1 !^(index\.php|resources|uploads|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Order of rules in .htaccess file

I have an .htaccess file like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foo.*$ /cgi/foo.cgi [L]
RewriteRule ^.*$ /cgi/fallback.cgi [L]
but when I go to a URL starting with foo in that folder, the browser still gets redirected to the fallback.cgi script. If I remove the second rule, the 'foo' line works OK.
According to my understanding, the first rule should take precedence, and the [L] should prevent any other rules from happening.
You are right, the 1st rule should be applied first and I think it is. The problem might be the 2nd rule is also being applied, so the code should be like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foo.*$ /cgi/foo.cgi [L,NC]
RewriteCond %{REQUEST_URI} !/cgi/(foo|fallback)\.cgi [NC]
RewriteCond %{REQUEST_URI} !/foo [NC]
RewriteRule ^.*$ /cgi/fallback.cgi [L]
The ^ means beginning of string.
RewriteRule ^foo.*$ /cgi/foo.cgi [L]
So that rules ONLY matches /foo followed by zero or more characters.
To match a file beginning with "foo" use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^|/)foo.*$ /cgi/foo.cgi [L]
It matches:
/foo
/foo.php
/food.php
/foolish/dog
It does not match:
/kung-foo.cat
/FOO.php
Update
Remember that some browsers do redirect caching. I was testing this on my own server and had performed a redirect that was cached. Made me confused for a bit when my new rules weren't working.
It is applying 2nd rule because;
In 2nd rule your are matching .* (means everything)
RewriteCond lines are only being applied to 1st rule only
Correct code would be:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# don't do anything for a file, dir or symlnk
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule (^|/)foo /cgi/foo.cgi [L,NC]
RewriteRule ^ /cgi/fallback.cgi [L]

Use specific PHP file for specific directories

I am trying to create mod_rewrite rules that rewrite based on the first level directory name plus a failover to rewrite to a standard file in case none of the directory names are matched.
Example:
I have units.php, models.php and other.php. The other.php file should handle all non-assigned requests.
http://www.mydomain.com/units/4435
Should redirect to /units.php?id=4435
http://www.mydomain.com/models/594
Should redirect to /models.php?id=594
http://www.mydomain.com/anything
Should redirect to /other.php?id=anything
http://www.mydomain.com/anything/893
Should redirect to /other.php?id=anything/893
Let me know if this makes sense. I am unsure of how to structure the rules and conditions to achieve what I want.
This is what I have tried to sfar. It works for URLs starting with 'units' or 'models' but I get a 500 Error if I try any other URL:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(units)/(.+)$ /units.php?id=$2 [L,NC]
RewriteRule ^(models)/(.+)$ /models.php?id=$2 [L,NC]
RewriteRule ^(.+)$ /other.php?id=$2 [L,NC]
I would suggest this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(units|models)/([0-9]+)/?$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(units|models)/[0-9]+/?$
RewriteRule ^/[^/]+/([0-9]+)/?$ /other.php?id=$1 [L,QSA]