Can htaccess RewriteRule be used to send data, rather than a URL? - .htaccess

For the fun value of it, can htaccess rewriteRule be used to return content, rather than a URL?
If I add the following to htaccess file
RewriteRule ^.*\.gif$ data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D [R=200,L]
Apache treats it as a relative URL, and rewrites the result as
data://domain-name/image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D
From a quick look at the source code, this behavior seems to be hard coded. Is there a way to do it?

Related

htaccess rewrite to new domain and url starting and ending with

My site's server is in the U.S. but I want to load images (and maybe later js and css) from a server in the visitors' country itself. I'm wondering what's a good way of rewriting only the images' urls that are in a specific directory.
Current url
http://www.myusserver.com/wp-content/uploads/image-name.jpg
url I want to use
http://www.myvisitorsserver.com/wp-content/uploads/image-name.jpg
I found this rewrite rule that does the job
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.myvisitorsserver.com/$1 [R=301,L,NC]
but I want to limit it to only images that are in the /wp-content/uploads directory.
I changed the rewrite to this
RewriteRule ^wp-content/uploads/ http://www.myvisitorsserver.com/wp-content/uploads/$1 [R=301,L,NC]
I think it's working, but I'm wondering if it's possible to rewrite only image urls. So, basically what I need is is to know how to rewrite and url starting with /wp-content/uploads and ending with an image extension.
I like this rule
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.myvisitorsserver.com/$1 [R=301,L,NC]
but I don't know how to change the first part of it to match /wp-content/uploads
Can anyone help me with this?
Thank you
I want to rewrite to the url structure as I mentioned in the 2nd example
Then just prefixing your rule pattern with the static path should work:
RewriteRule ^(wp-content/uploads/[^.]+\.(jpe?g|gif|bmp|png))$ http://www.myvisitorsserver.com/$1 [R=301,L,NC]

How do I htaccess redirect all my urls containing %23 replacing this with #

I would like to redirect all my urls replacing %23 with a # in my htaccess file
Example: http://afdelingtest.nl/vierdaagsefeesten/programma/%23jaap-categorie should be http://afdelingtest.nl/vierdaagsefeesten/programma/#jaap-categorie
The last part 'jaap-categorie' differs.
I tried many example Htaccess lines but nothing seems to work. Like: RewriteRule ^(.#.) /$1 [R,NE]
Can somebody help me achieve this?
Best, Jaap
This expression might help you to write a RewriteRule and do so:
^(.*)(%23)(.*)$
The hex code for hashtag is \x23. You want to replace it with $1\x23$3.
This graph shows how that expression would work:
Your RewriteRule might look like something similar to:
RewriteRule ^(.*)(%23)(.*)$ $1\x23$3 [NE,R=301,L]
or
RewriteRule ^(.*)(%23)(.*)$ $1\x23$3 [NE,R=302,L]
302 is temporary and 301 is permanent, however you wish. You might want to do a 302, then change it to 301.
You might want to clear your browser cache every time you change your .htaccess file.
I'm assuming that you have one %23 in your URLs.

Internally Redirect URL without 302 status using htaccess

I am new to PHP. I created a website which creates a multiplication table from given input number.
Now I want that instead of submitting the input, a number multiplication table should be accessible through URL.
i.e. www.multiply.com/multiplication-of-13.php.
Creating a single page for every doesn't seem like a feasible solution,
So I did it using the .htaccess file by changing the query parameter to URL.
Example :
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^multiplication\-of\-([0-9]+)\.php$ index.php?num=$1 [R]
Now the problem is this configuration is generating 302 redirect, which is not suitable for SEO, and if I use [R=301] then the changes reflect in the URL, which does not seem user-friendly, Is there is any possible solution that the URL redirection doesn't return HTTP Code 302?
Take off the [R] after the URL. That means "redirect," which is explicitly what you are asking not to happen.
The [R] in your rewrite is causing it to generate a 302.
RewriteRule ^multiplication\-of\-([0-9]+)\.php$ index.php?num=$1 [L, QSA]

htaccess redirect dynamic urls

I am trying to write a htaccess redirect, but it is not working as I want it to.
My current (working) htaccess redirects all html pages with 1 character to the index file as:
RewriteRule ^([a-zA-Z0-9]).html$ index.php?letter=$1
So a.html gets redirected to index.php?letter=a
Now I need to redirect the page a.html?page=2 to index.php?letter=a&page=2
Basically I want to redirect the url, but leave the dynamic part intact.
All of the following return a 404:
RewriteRule ^([a-zA-Z0-9]).html?page=([0-9]+) index.php?letter=$1&page=$2
RewriteRule ^([a-zA-Z0-9]).html?page=(.*) index.php?letter=$1&page=$2
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule ^([a-zA-Z0-9]).html(.*) index.php?letter=$1&page=%1
I think I'm close, but I can't seem to get there :/
could anyone give me the last push?
Your RewriteRule needs to be
RewriteRule ^([a-zA-Z0-9])\.html$ index.php?letter=$1 [QSA,NC,L]
Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.
Please, note that the dot before html needs to be escaped \. as well. The [L] makes sure that rewriting stops and no further rules (if any below) are applied.

How to rewrite any page to index page with requested URL as $_GET parameter

Something similar had been discussed here, but this is slight different.
How to tell .htaccess to rewrite any requested page (regardless of it's depth from the site root) with site's index page, but with requested URL as a $_GET parameter, so it can be further handled by php, depending on it's contents, or depending on URL itself.
I was trying something like
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*) index.php?page=$1
but obviously I do something wrong.
Thanks in advance.
Try working on your RegEx. Maybe something like this:
RewriteRule ^([a-zA-Z0-9\-]+)([\/]*)$ index.php?page=$1 [NC,QSA,L]
Basically you can generate the rewrite code using an online tool like http://www.webtoolhub.com/tn561403-htaccess-url-rewrite.aspx by entering the desired parameters. Just put the url structure and it will write the .htaccess code that you can use or modify according to your needs.

Resources