RewriteRule - Check if certain Term in URL exists, if not add it - ajax

I am currenctly facing some htaccess/rewriterule issues. (And I am new to this area)
Let's assume we have an url like this:
http://mypage.at/very/cool
The URL is supposed to look like this (Cause I am using an AJAX-loadedContent which requires this):
http://mypage.at/#ajx/very/cool
So I would like to add the part '#ajx' to every url which does not already contain it.
Which means if an url does already look like: http://mypage.at/#ajx/so/pretty then there is no need for changes.
As I am not sure wheter this creates troubles with the GoogleSearchIndex, I would additionally like to know if there is a way to exclude this rule for searchbots.
Thanks for any help.
Ripei

Since you reported that this does not work (which is probably because your version of Apache doesn't support Perl-style RegEx):
RewriteRule ^(?!#ajx)(.*)$ http://mypage.at/#ajx/$1 [L]
I think this should do it:
RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*)$ http://mypage.at/#ajx/$1 [L]
EDIT: After trying this myself and reading around on the Internet, I'm not sure this is actually possible. A pound sign (#) is not a legal part of a URL. This answer comes close, but I'm going to have to leave this to somebody who knows more to say whether this can even be done the way #Ripei asked for.

Something like this might work:
RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*) http://%{SERVER_NAME}/#ajx$1 [R,L]

Hah finally after lot's of reading and testing - I got it.
RewriteCond %{REQUEST_URI} !^#ajx
RewriteRule . /\#ajx%{REQUEST_URI} [L,R,NE]
This Code works for me... don' ask me why this one is working, to be honest I do not have a clue! But well I am fine with the fact, that it IS working :)

Related

Rewrite second URL Parameter htaccess

I have searched in SO but never came across this specific answer. If anyone can help.
This use to be my URL structure /index.php?param1=is1 and I got it to look like this /is1 by using this Rewrite.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
Do not ask my how I got this as it's been a while that this was written. Now the issue is i have a second parameter. So my url that is like this /index.php?param1=is1&param2=is2 needs to be like this /is1/is2.
Obviously, now it's like this /is1&param2=is2. So i guess what I am searching for is a way to ask if &param2= is seen than hide it ?
I really don't get the logic behind all this URLREWRITE. I am trying to clean my URL structure for SEO purposes and COPY/PASTE logic as URL's should be shared between different users / clients. Obviously, having /this-is-something&this=that is not that bad but having /this-is-something/that is much better.
Just for info, first parameters is BRANDS /this-is-my-brand1 or /this-is-my-brand2 and second parameter is a product as /this-is-my-brand1/product1 before all that I use to have /index.php?brand=brand-one&product=product-one. Changing everything is not an issue, I can start from scratch if there is anything better than what I have.
I am on the learning curve, so I don't mind long explanations.
Any help is appreciated.
If your request parameters name are static (never change) you can use a more specifiq rule
RewriteRule ^(.+)/(.+)$ index.php?brand=$1&product=$2 [L]

301 redirect with RewriteRules

I have a problem concerning RewriteRules.
I'd like to move one page permanently, so I want to use a 301 redirect. I tried this:
RewriteRule ^page1/([A-Z].*)$ http://www.abs.nl/page1/vraag-$1 [R=301]
However this does not work. Can someone please tell me how I could fix this? I already tried for hours to find an answer.
Should I also use a %{HTTP_HOST} condition? I see this a lot but I don't know how it works.
A couple things:
Make sure that you have RewriteEngine On before your rewrite rule.
Your current regex is looking for a capital letter followed by any character, is this what you want?
Try this:
RewriteEngine On
RewriteRule ^page/(.*)$ http://www.abs.nl/page1/vraag-$1 [R=301,L]
That redirect will match on any string of characters following page/ in a URL.

using mod_rewrite to point at a blog

I'm trying to use mod_rewrite to point the blog portion of a site to a blog site.
this is what I have to handle the normal stuff
RewriteRule ^(\w+)/?$ index.php?page=$1
This is what i'm trying to use for the blog site
RewriteRule ^blog/?$ http://url.to.my.blogger.site
but it's not working, when I go to site/blog it directs me to index.php?page=blog is there something I need to do to not do the second rewrite if the first is correct? like an if/else? sorry don't know much about mod_rewrite so any advice would be awesome.
also I noticed that if I try to do something like site/home everything works fine but if I attempt to hit site/home/ it puts all of my urls into the wrong context, for example my css and images don't get loaded correctly.
my full file is this
RewriteEngine on
RewriteRule ^blog/?$ remote/blog/uri/here
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php - [F]
and when i hit site/blog it still tries to serve index.php?page=blog, I'm guessing I have to break out of the code at some point? I couldn't find documentation on if/else statements
I needed to add flags to my RewriteRule lines so that the server wouldn't evaluate further. Changing them to be
RewriteRule ^/blog http://url.to.blog [L]
did the trick, the problem was that it was evaluating all the way down, seeing as I wasn't attempting to go to index the last valid rule to evaluate was the general rewrite rule.

Mod rewrite rule for cached pages

I am caching pages in my (Rails) application based on subdomain. The pages for certain actions are cached to /public/cache/(subdomain)/. The application is running under Apache with Phusion Passenger. The caching is working fine. The problem is that Apache is not picking up the cached pages and bypassing Rails like it should be. My rewrite rules are wrong and I need help fixing them.
I have used, as one example of many, the suggestion located at: https://github.com/yeah/page_cache_fu#readme, which is as follows:
RewriteMap uri_escape int:escape
<Directory /var/www/example.com/current/public>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET [NC]
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING}.html -f
RewriteRule ^([^.]+)$ cache/%{HTTP_HOST}/$1${uri_escape:%{QUERY_STRING}}.html [L]
RewriteCond %{REQUEST_METHOD} GET [NC]
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/index.html -f
RewriteRule ^$ cache/%{HTTP_HOST}/index.html
The problem with this is it seems to be expecting the directory to be the full http host (i.e. it's looking in cache/subdomain.example.com rather than just cache/subdomain).
Edit: Even when I change the Rails app to cache to cache/subdomain.example.com Apache still does not use them so it seems that there is more wrong than just the subdomain aspect.
Could someone please help me come up with the correct rule?
Edit(2):
I have simplified my rewrite to the following (just to try to get to a working starting point):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond ^stats$ cache/%1/stats.html [L]
I would think this would cause http://abc.example.com/stats to be rewritten to http://abc.example.com/cache/abc/stats.html
It is not. I also added a RewriteLog entry and what I see there makes me think it is trying to redirect to http://abc.example.com/var/www/example.com/current/public/cache/abc/stats.html. This is further confirmed by that if I add an 'R' option along with the 'L' I see in my browser http://abc.example.com/var/www/....etc. I.e. it seems to be appending the full document root instead of just the public facing part.
Of course the result of the above is that I get a 404 error returned to the browser.
Can you see what is still wrong with my rule?
Edit: It's actually a bug.
http://code.google.com/p/phusion-passenger/issues/detail?id=563
Alright, this looks like it should work, but it doesn't. I've done a lot of testing with this, and it seems like the problem is the ^([^.]+)$ in the RewriteRule. Now, I did Google this, and it seems like it's a common enough pattern, so I don't understand what the issue could be. I just know that when I use that pattern in a RewriteRule, the rule fails. If I change it to ^([^.]+), it seems to work.
Hopefully someone with more experience with mod_rewrite can come along and explain to us what the problem with that pattern might be.
Edit: I just realized the problem with ^([^.]+)$:
Since you're building a cache, then the "normal" file will exist in its usual place. The implication of this is that if you ask the server for /file then, depending on your configuration, it will say "hey, file doesn't exist, so let's try the default extension of .html!" and so it goes off and finds file.html. Now when you get to the RewriteRule, the ^([^.]+)$ regex will be matched against file.html NOT file.
The ^([^.]+)$ says "the start of the string, followed by as many non-period characters as you can grab, followed by the end of the string" which works fine against file because it contains no periods. It fails against file.html because ^[^.]+ will match against file, but where the regex then expects to find the end of the string (i.e. $), it instead finds .html and fails.
The reason ^(.*)$ works is that it's guaranteed that only .* will be the whole of the string, since .* matches "as many of any character" so there is no character that can possibly exist between the (.*) and $ portions of the regex. That's not the case with [^.]+.
In order to extract the subdomain, you're going to need to backreference a RewriteCond. Basically, if you capture a reference (i.e. encapsulate something inside parens) in a RewriteCond, those references are available to a RewriteRule which immediately follows it.
For example, if I wrote this:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com
Then the parentheses would capture the subdomain - note the () around [^.]+
If I were then to write a RewriteRule on the next line, the text captured above would become accessible as %1.
So your RewriteRule would look like this:
RewriteRule ^([^.]+) cache/%1/$1${uri_escape:%{QUERY_STRING}}.html [L]
Hope that helps.

How do I make an exception to a mod rewrite rule?

I absolutely do not understand mod rewrite or the syntax to make it work. I have however managed to cobble together the following, which works exactly as I need it to on my site:
RewriteRule ^page/([^/\.]+)/?$ page.php?page=$1 [L]
it changes www.mysite.com/one into www.mysite.com/page.php?page=one
wonderful.
However, is there a way that I can add an exception to the rule, so that if I try to use the url www.mysite.com/feed for example, it will go to www.mysite.com/feed/index.php rather than trying to redirect to www.mysite.com/page.php?page=feed
Thanks in advance!!
There are a couple of ways you could do this. Probably best would be to add a RewriteCond above the RewriteRule. Something like this (untested):
RewriteCond !^feed
RewriteRule ^page/([^/\.]+)/?$ page.php?page=$1 [L]
If you can't get that to work then you could also put a different RewriteRule above your one to redirect before this rule is matched...

Resources