Mod_Rewrite: Transform normal /url into /#!/url - ajax

I have the following two url cases …
http://url.com/one
http://url.com/category/some
If those urls are called I want mod_rewrite to call …
http://url.com/#!/one
http://url.com/#!/category/some
How can I do that?

Something like this?
# not existing file (images, css, etc)
RewriteCond %{REQUEST_FILENAME} !-f
# no query parameters
RewriteCond %{QUERY_STRING} =""
# not /
RewriteCond %{REQUEST_URI} !^/$
# external redirect and pass along query string and uri as fragment
# i guess this must be an external redirect as the server side should
# not see the fragment, R=redirect, NE=dont escape #, L=last rule
RewriteRule ^(.*)$ /#!/$1 [R,NE,L]
# same but with query parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /?%{QUERY_STRING}#!/$1 [R,NE,L]
But im not sure if this is good idea. Maybe you should do the redirect in application logic or with a client side script instead.

Related

how to create an empty rewrite rule, that will not modify the url

I have static files in several directories on my website.
I was trying to create a rule that will instruct the rewrite engine to disregard all of those static files.
something like this:
#RewriteCond %{REQUEST_URI} !\.*(jpg|css|js|gif|png|swf)$ [NC]
this seems to work only in some of the cases, but not all of them.
As a solution I decided to match the entire request URI of any requested file with one of the following extensions, and create an 'empty' rewrite rule for them, that will redirect the user to the precise request URL, basically.
To achieve that I did the following:
RewriteCond $1 \*.(js|ico|gif|jpg|png|css|swf|mp3|wav|txt)$ [NC]
RewriteRule ^(.*) \$1 [R=301,L]
This won't work for some reason, no useful information at the access.log and error.log either.
Thanks in advance for any assistance!
Full code:
#Catch static content and don't modify it
RewriteCond $1 \*.(js|ico|gif|jpg|png|css|swf|mp3|wav|txt)$ [NC]
RewriteRule ^(.*) \$1 [R=301,L]
#prevent looping
RewriteCond %{QUERY_STRING} !clientId=
#redirect sub domain to the client page cutl.mysite.com = mysite.com/in/index/php?clientId=curl
RewriteRule ^(.*)$ /in/index.php?uclinetId=$1 [L]
Rather than maintaining a long list of file extensions for exclusion you can simply use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
Which means apply next RewriteRule if request is NOT for a real file or a symbolic link.
Probably your backslash should be between the asterix and the dot?
RewriteCond $1 *\.(js|ico|gif|jpg|png|css|swf|mp3|wav|txt)$ [NC]
RewriteRule ^(.*) \$1 [R=301,L]

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

rewrite condition not working after stripping query string

I am using iirf on IIS6 on a Windows 2003 server. We have the following code to present clean urls and to remove any query strings:
RewriteRule ^/(.+)\?(.+)&(.+)\.(htm)$ /$1
RewriteRule ^/(.+)\?(.+)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# Redirect to ASP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.asp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]
RedirectRule ^/(.+)\.(asp)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# Any bare URL will get rewritten to a URL with .htm appended
RewriteRule ^/([\w]+)$ /$1.htm [I,L]
RedirectRule ^/(.+)\.(htm)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
The problem is that since I have added
RewriteRule ^/(.+)\?(.+)&(.+).(htm)$ /$1
RewriteRule ^/(.+)\?(.+)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
All query string parameters are removed where we actually need them for(sorry - urls not available externally):
betatest.bracknell-forest.gov.uk/news.asp?page=2
In fact the only query string condition we want to to strip the parametrs for is if it contains the Facebook parameter fb_action_ids=52352315213 e.g.
betatest.bracknell-forest.gov.uk/help?fb_action_ids=372043216205703&fb_action_types=og.likes&fb_source=aggregation&fb_aggregation_id=288381481237582
I have tried using:
RewriteCond %{QUERY_STRING} ^ fb_action_ids=(.)$ [I]
before the first pair of rules but but it does not appear to do anything.
Just managed to resolve it and boy it feels like a burden off my shoulders:
#key thing I have changed is to specify the query string parameters fb_action_ids and fb_source
RewriteRule ^/(https?)://([^/]+)(/([^\?]+(\?(.*))?)?)? /$1
RewriteRule ^/(.+)\?(fb_action_ids=(.*)|fb_source=(.*))$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# e.g. example.com/foo will display the contents of example.com/foo.asp
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]
RedirectRule ^/(.+)\.(asp)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]
# Any bare URL will get rewritten to a URL with .htm appended
RewriteRule ^/([\w]+)$ /$1.htm [I,L]
RedirectRule ^/(.+)\.(htm)$ http://betatest.bracknell-forest.gov.uk/$1 [R=301]

Friendly URL for images

Please suggest me what to put into .htaccess file.
I have PNG, GIF, JPG images on server http://domain.tld/images/anyimage.anyextension
Want to make URLs more friendly like a http://domain.tld/anyimage.anyextension
This I have now. First two strings change links as described. But last string doesn't change it back for server.
RewriteCond %{REQUEST_URI} ^/images/(.+)(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)$ /images/$1 [L]
If I add
RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/image/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /image/$1 [L,NC]
Right after previous query string rule then images don't open. If before there's no problem. What it could be? Do you have an idea how to fix it? The last string RewriteRule ^/?(.+)$ /?$1 [L] cause this conflict
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+) [NC]
RewriteRule ^$ /%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)$ /?$1 [L]
Not sure how that makes it more friendly, aside from it being shorter. You can try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/images/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /images/$1 [L,NC]
Then you can change all of your links from http://domain.tld/images/anyimage.anyextension to http://domain.tld/anyimage.anyextension
The first condition checks to make sure the request is for anyimage.anyextension, as long as the extension is a case-insensitive png, gif, or jpg.
The second condition checks to make sure the requested image actually exists in the /images/ directory.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /images/(.+)(\.gif|\.jpg|\.png) [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]
This redirects the browser to the non-/image/ URL. There's 2 completely different things going on here. One deals with the browser and the other deals with content on the server. See the top part of this answer that explains how they are different.
The rules that you have won't work. First:
RewriteRule ^/(.+)$ /images/$1 [L]
will never match. URI's used to match against the regex of a RewriteRule in htaccess files have the leading slash removed, so no URI is going to start with a /. You need to get rid of it.
Secondly, once you do, you'll get a 500 internal server error because your rules will cause an internal infinite loop. You need to match against %{THE_REQUEST} to ensure that a browser is actually requesting a URL with the /images/ path in it, not what has been internally rewritten.

How do I write a custom mod_rewrite .htaccess file for CakePHP routing?

I've rewritten my web app using CakePHP, but now I need to have my old formatted urls redirect to my new url format. I can't seem to add my own custom mod rewrite rule. I've added it above the main cakephp rewrite rule, but I'm getting an infinite redirect loop. I just want http://mysite.com/index.php?action=showstream&nickname=user to redirect to http://mysite.com/user before the cakephp rewrite happens.
EDIT: Ok, so now when the condition is met it's redirecting but it's appending the original query string to the end. I'm assuming that's due to the QSA flag in CakePHP rewrite rules, but I was under the impression the "L" in my rule would stop that from executing...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action\=showstream&nickname\=(.*)$
RewriteRule ^.*$ http://mysite.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When you do a capture inside the RewriteCond line instead of the RewriteRule, you have to reference the capture with %N instead of $N. That is, your RewriteRule line should be:
RewriteRule ^index.php$ /%1 [R=301,L]
Try to test the request line (THE_REQUEST) to see what URI originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php
RewriteCond %{QUERY_STRING} ^action=showstream&nickname=([^&]*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
But maybe it would be easier to do this with PHP.

Resources