Rule in htaccess file - mod-rewrite

Somebody could help me for this php rule "RewriteRule ^((?s).*)$" what's that mean?
I did this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.html?_url=/$1 [QSA,L]
Thanks!!

That pattern means match the entire string, the (?s) is an internal option setting to ensure that . also matches line break characters, which is not necessary.

Related

Relative path and mod_rewrite issue

Have a trouble with mod_rewrite.
I want :
htp://example.com/a/some_text -> htp://example.com/?p1=some_text and
htp://example.com/b/some_text -> htp://example.com/?p1=some_text
So, I type:
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^(.*)$ ?fsearch=$2 [QSA]
and get wrong relative paths in css, such as htp://example.com/a/CSS/main.css instead of htp://example.com/CSS/main.css. And get nothing in $2 too.
Help, please
$2 tries to pick the second capture group of the RewriteRules subject, but there is only one! You probably mean %2 which picks from the RewriteCond...
Since you only append a query string in your RewriteRule the original URI (path) is left untouched.
You can simplify the pattern in the RewriteRule since you are not interested in the subject anyway.
This is probably what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /?fsearch=%3 [QSA]
Much better however would be to rewrite directly to whatever script you actually want to process the request to safe another rewriting round to find the index document, so something like:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%3 [QSA]
And unless you decide to use the first capture group in the RewriteCond you can also simplify that further:
RewriteEngine on
RewriteCond %{REQUEST_URI} \/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%2 [QSA]

Assistance with .htaccess

I need the .htaccess file to allow all file and directories if they exist, but php extensions are not needed for existing files and everything else goes to the index file.(MVC type processing) I've tried a few things, but haven't got it right yet.
Examples:
www.example.com/search/
file exists as search.php and should display the file
www.example.com/shopping/mylist/file doesn't exist so should go to index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
mod_rewrite is highly order dependent so let's think through it logically from most specific to least specific.
You will need a condition to check for the existence of the .php file based on a match from the () group in RewriteCond first. That will be two conditions followed by a RewriteRule to actually direct it into the .php file. Your example /search/ has a trailing slash, and that's why we'll first need to capture it as %1 with two RewriteCond. Otherwise, I would probably use %{REQUEST_FILENAME}.php -f to test if it exists. This sort of explains how the %1 backreference can be used in RewriteCond chain.
Then after applying that one to attempt to match a .php file, use the more generic index.php rule as you already have it, together with the two conditions to check whether the file actually exists.
RewriteEngine On
# Match an optional trailing slash on the filename
# and capture it as %1
RewriteCond %{REQUEST_FILENAME} ^(.+)/?
# And test if the match (without /) has a .php file
RewriteCond %1.php -f
# Rewrite everything up to an optional trailing /
# matched in the first RewriteCond
# into its .php suffix (add QSA to retain query string)
# It isn't necessary to give a full regex here since %1
# already contains everything needed
RewriteRule ^ %1.php [L,QSA]
# Now with that out of the way, apply the generic
# rule to rewrite any other non-existing file to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# I used * instead of + so it also matches an empty url
RewriteRule ^(.*) index.php?url=$1 [QSA,L]
I have successfully tested this setup in a temporary directory. It appears to meet your requirements.

RewriteCond Simple Issue

I need the URl http://mydomain.com/careers to go to http://mydomain.com/#!/careers
For what it's worth I've tried numerours variations around this with no success
RewriteCond %{REQUEST_URI} !^/careers$
RewriteRule (.*) /#!/careers [QSA,L]
Can anyone help?
"!" in your RewriteCond line means "not"...
Also, "[QSA,L]" means:
L means this is last rule (processing terminates after matching this one) and
QSA means query string append
But, becase R flag was not specified, this is done by sub-request and not a redirect, so the actual URL in your browser does not change...
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/careers$
RewriteRule (.*) /#!/careers [R,L,QSA]
Hope this helps

Rewrite rule fails to match single quote chars

I asked this question earlier:
mod_rewrite: match only if no previous rules have matched?
And have been using the suggested solution with success for a while now:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^.+\ (/[^?\s]*)\??([^\s]*)
RewriteCond %{REQUEST_URI}?%{QUERY_STRING}<%1?%2 ^([^<]*)<\1$
RewriteRule .* /pub/dispatch.php [L]
However, we've since discovered that this rule fails for URLs containing single quote chars, e.g. http://example.com/don't_do_it (which is actually requested as http://example.com/don%27t_do_it)
Specifically, this is the line that's failing to match:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING}<%1?%2 ^([^<]*)<\1$
commenting it out causes the rule to match as expected, but breaks the "match only if no previous rules have matched" behavior. This is presumably related to the fact that ' is urlencoded to %27.
Here's the relevant RewriteLog entry (for the url /asdf'asdf aka /asdf%27asdf):
RewriteCond: input='/asdf'asdf?</asdf%27asdf?' pattern='^([^<]*)<\1$' => not-matched
What I'm seeing here is that %{REQUEST_URI} is unescaped while %{QUERY_STRING} is escaped, hence the mismatch. Is there an alternative to either one of those I should be using?
Any ideas how to rewrite the above line so that it will also match lines that contain ' chars?
Try the C flag and chain the sequence of rules of which you just want one to be applied. So actually chain all of your rules.
You can test the [NE] flag at the end of the RewriteRule.
After beating on it for quite some time, things are looking good with:
RewriteMap unescape int:unescape
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^.+\ (/[^?\s]*)\??([^\s]*)
RewriteCond %{REQUEST_URI}?%{QUERY_STRING}<${unescape:%1}?%2 ^([^<]*)<\1$
RewriteRule .* /pub/dispatch.php [L]

mod_rewrite trailing slash with RewriteCond

I have searched through the related mod_rewrite qustions but I can't anything specific enough so I'll post:
Heres my rule that adds a trailing slash:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
Only I want to exclude one specific directory eg. /mydirectoryname/ and not add the trailing slash to anything that starts with that. Reason being its breaking some of my ajax calls.
Add another RewriteCond:
RewriteCond %{REQUEST_URI} !/mydicrectoryname)/
You can descibe that with just one condition:
RewriteCond $0 !^mydirectoryname(/|$)
RewriteRule ^[^\.]+[^/]$ /$0/ [R=301,L]

Resources