.htaccess image to web page redirect? - .htaccess

Is there a .htaccess script I can use to redirect the url of an image to an actual web page?
What can I do to make it so when someone accesses an image via their address bar by typing in: http://www.sitename.com/images/1.jpg
It will instead redirect the user to the web page:
http://www.sitename.com/view/1.html
I want to still be able to place the images in image tags though.

RewriteCond %{HTTP_REFERER} !^https?:\\www\.yourdomain\.com
RewriteCond %{REQUEST_URI} (.*)\.jpg$
RewriteRule images/(\d+)\.jpg http://www.yourdomain.com/view/$1.html [L,R=301]
by using this you can put images in img tag and it will work but If you try to access them directly , it will redirect to html file.
Note: If you want others have permission to use your images use this:
RewriteCond %{HTTP_REFERER} ^$

Related

How to redirect HTML index file to subfolder directory?

I have created a subfolder to access my HTML website. But the problem is, it is not running on the subdirectory URL but with the index.html aliases.
e.g. https://example.com/my-html-site/index.html (the website running the whole page).
But I want a simple redirection to run the website under the subdirectory like this below,
https://example.com/my-html-site/ (when I hit this URL then it shows a blank page).
Is there any way to redirect the https://example.com/my-html-site/index.html to https://example.com/my-html-site/ via htaccess file rules?
Thanks in advance,
Based on your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.html [L]

How to add .htaccess URL mask so that specific pages are shown to be under subdomain

I got a site with some pages:
http://example.com/myuglyurl.php?id=11
http://example.com/myuglyurl.php?id=12
http://example.com/myuglyurl.php?id=13
and, just for some pages, I want my users to see in their browsers that they are in subdomain, for example:
http://example.com/myuglyurl.php?id=11
http://mynewsubdomain.example.com/myuglyurl.php?id=12
http://example.com/myuglyurl.php?id=13
Subdomain exists and when user directly tries to visit subdomain, I'll redirect them to specific page, that is no problem. But the URL mask I cannot make.
I've tried all kinds of .htaccess tricks, but as I am a complete stranger to .htaccess, I cannot manage to do it.
I've tried:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{QUERY_STRING} (^|&)id=12($|&)
RewriteRule ^myuglyurl\.php$ http://mynewsubdomain.example.com/myuglyurl.php?%{QUERY_STRING}
But I get blank page with message: File not found.
Please use this following.
Redirect and keep everything after the URL
Visit yourdomainA.com/page and it will show the content from yourdomainB.com/page
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomainA.com
RewriteRule ^(.*) http://www.yourdomainB.com/$1 [P]
For more details, please refer to this link - https://www.brontobytes.com/knowledgebase/202/htaccess-URL-Masking-Examples.html

htaccess rewrite url, add dir and remove query

I have two versions of images on my server, one with and one without a watermark. On my one website I want to have to non-watermarked version. On the websites that hotlink my images I want to have the ones with the watermark.
I want to rewrite the url to serve the watermarked images on those website. The url structure on my website is this:
www.example.com/wp-content/uploads/image-name.jpg?t=1512472796
and I want to change it to:
www.example.com/wp-content/uploads/wm/image-name.jpg
I am kind of stuck here, no idea to do the part that adds the wm dir to the url and removes the query:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{QUERY_STRING} ^t=([0-9]*)$
Any help is highly appreciated.

Redirect direct image access

Im struggling with this problem and i know there are some solutions but none is working fine for me. I want to redirect direct image access to its HTML site because the image filename is exact the same like the HTML site. Example:
http://example.com/images/subcategory/this-is-an-image.png
should be redirected to
http://example.com/images/subcategory/this-is-an-image.html
The image can be an png, jpg, jpeg or gif. And of course i still want to embedd the images in <img> tags on my site. The goal is not to prevent hotlinking, the goal is to redirect all direct image calls on my server to its html site. Are there any solutions? Thank you.
You can use that:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)example\.com [NC]
RewriteRule ^(.+)\.(jpe?g|gif|png)$ $1.html [NC,R,L]
Not 100% reliable method, but better than nothing...

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources