What is the meaning of %{HTTP_REFERER} !^$ in a RewriteCond? - mod-rewrite

I've come across
RewriteCond %{HTTP_REFERER} !^$
Does this just mean "not empty"?
Thanks

Yes, you are correct, all that that line is doing is ensuring that the REFERER header is not empty. Logically, recall that $ indicates simply the end of a string to be matched, so this !^ is just ensuring there isn't nothing there.

Related

Mod_Rewrite for URL

I have been looking through questions and answer for days trying to figure out how to make this work.
So far I can get my URL to change, but it won't load the page.
I have to take
http://www.mysite.com/index.php?mode=about
And have it show up as
http://www.mysite.com/about/
So far I have the following code:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^mode=(.*)
RewriteRule ^ http\:\/\/\www.mysite.com\/%1? [R=301,L]
RewriteRule /(.*) /index.php?mode=%1 [L]
I have changed things multiple times and nothing. Most site seem to tell me I don't need the 301 redirect but then I can't get anything to work.
For (your) example, once you've properly routed from mysite.com/index.php?mode=about to mysite.com/about, it's now going to look at mysite.com/about/ to find what comes next (index.py/index.html/etc).
Because there is nothing at /about/, you're getting a 404 error.
I don't think you can use mod_rewrite to do exactly what you're trying to achieve, without having some handling within /about/ to actually display the page you want once you get there.
http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html
Remember the Filesystem Always Takes Precedence
The filesystem on your server will always take precedence over the
rewritten URL. For example, if you have a directory named “services”
and within that directory is a file called “design.html”, you can’t
have the URL redirect to “http://domain.com/services”. What happens is
that Apache goes into the “services” directory and doesn’t see the
rewrite instructions.
To fix this, simply rename your directory (adding an underscore to the
beginning or end is a simple way to do that).
I have to take
http://www.mysite.com/index.php?mode=about
And have it show up as
http://www.mysite.com/about/
There are two very common types of rules that people want and your statement can be interpreted two ways which require different rules. I'm going to interpret your statement that you have a real, operational script at http://www.mysite.com/index.php?mode=about, but instead of having the user enter that "ugly" URL, you want them to be served that URL when they enter http://www.mysite.com/about/. To accomplish this, you would do the following:
RewriteRule ^about/?$ /index.php?mode=about [L]
Because of the potential for misunderstanding, it's best to state what you want as (1) What the user will enter into their browser and (2) what real file you want to serve them.
I don't believe you need lines #2 & 3 & you seem to have % instead of $, try:
RewriteEngine on
RewriteRule ^([^\/]+) /index.php?mode=$1 [L]
Solved the problem. Thanks for all the help.
#< IfModule mod_rewrite.c>
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
#< /IfModule>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?mode=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?mode=$1
You can use this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ /index.php?mode=$1 [L]
Where the beginning still allows for other folders to be accessible.

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

mod_rewrite, trailing slash removal, and current directory

I've perused the related questions, but I cant figure this little caveat out. The golden rule I've seen tossed around for trailing slash removal via mod_rewrite is:
RewriteRule ^(.*)/$ /$1 [R,L].
This is all fine and good, but it strips off the preceding directory structure given one exists. So, if my application bootstrap is running at the root of the hosted path, it works, but not if in a subdirectory:
http://localhost/path/to/application/pretty/query/string/
Becomes
http://localhost/pretty/query/string (Note; slash is stripped, but so is directory)
How can I preserve the current directory location, so the previous example returns the expected:
http://localhost/path/to/application/pretty/query/string
Update
Ultimately, this is for the sake of consistency; either appending or stripping a trailing slash is a suitable. I'm working back and forth between trying to get either approach to work, with no success.
Answers that either append or strip the trailing slash are acceptable!
Well, I solved it for appending:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /_dev/_projects/mf_frame
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) ?routePath=$1 [L,QSA]
I was missing the RewriteBase directive. I'll have another go and stripping. Please feel free to suggest alternatives to my approach if there's something that can be done better.

Simple mod rewrite question

Here is my current .htaccess file:
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
As you can see, requests to http://domain.com go to http://domain.com/index.html. I want to change this so that they go to http://domain.com/foo, please note that does not exist as a file or folder, it is handled by rails. How do I do this? Note that I have tried the following and it doesn't work:
RewriteRule ^$ foo [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Thanks!
You're probably safe to just change the first line to this:
RewriteRule ^$ foo [QSA,L]
The L flag tells mod-rewrite that it shouldn't apply any other rules after that one. The problem right now is that the second rule gets applied after the first one, and you end up at "foo.html", instead of "foo", right?
The difference between you trying to send to "foo" and the original redirect to "index.html" is that the second rule applies to requests that do not include a period. So when the first rule was redirecting to "index.html", after it was used, the second rule was no longer valid. However, now that you're not redirecting to a location with a period in it, the second rule gets applied after the first one, so you get a double-redirect.
In addition, you may be able to drop the QSA flag from the first line, it depends on your site though. If someone accesses the site like http://domain.com/?user=fred, do you want to send them to http://domain.com/foo?user=fred, or just http://domain.com/foo? If you don't need the Query String Appended, you can drop the QSA flag, and just have:
RewriteRule ^$ foo [L]

Mod-rewrite shenanigans

I have this rewrite rule
RewriteEngine On
RewriteBase /location/
RewriteRule ^(.+)/?$ index.php?franchise=$1
Which is suppose to change this URL
http://example.com/location/kings-lynn
Into this one
http://example.com/location/index.php?franchise=kings-lynn
But instead I am getting this
http://example.com/location/index.php?franchise=index.php
Also, adding a railing slash breaks it. I get index.php page showing but none of the style sheets or javascript are loading.
I'm clearly doing something very wrong but I have no idea what despite spending all day R'ingTFM and many online primers and tutorials and questions on here.
Your problem is you are redirecting twice.
'location/index.php' matches the regex ^(.+)/?$
You want to possibly use the "if file does not exist" conditional to make it not try mapping a second time.
RewriteEngine on
RewriteBase /location/
RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories
RewriteRule ^(.+)/?$ index.php?franchise=$1 [L,QSA]
And additonally, theres the [L,QSA] which tries to make it the "last" rule ( Note, this is not entirely obvious how it works ) and append the query string to the query, so that
location/foobar/?baz=quux ==> index.php?franchise=$1&baz=quux
( i think )
It sounds to me as though the rewrite filter is executing twice. Try adding a last flag
RewriteRule ^(.+)/?$ index.php?franchise=$1 [L]
Your rule self matches, and therefore it will reference itself.

Resources