I have problem with creating .htaccess rule for creating url for small resized images.
I have saved images inside upload/images/ directory and it looks like this:
somedomain.com/upload/images/bigimage-some_name123.jpg
I have already created url via PHP so i can call resized image:
somedomain.com/upload/images/small/bigimage-some_name123-150x100.jpg
From this small url I need 4 parameters:
name: bigimage-some_name123
extension: .jpg or jpg
width: 150
height: 100
Via .htaccess need to create url so I can fetch all parameters above and show resized image on the fly:
somedomain.com/image-resizer/index.php?image=upload/images/bigimage-some_name123.jpg&width=150&height=100
Colleague of mine send me this .htaccess line and I need to rewrite it so it can work on my project.
RewriteRule
^upload/images/small/([^/.]+)-([0-9]{3,5})([^/.]+)-([0-9]{2,3})x([0-9]{2,3}).jpg$
module/show_image.php?name=../files/$2$3.jpg&width=$4&height=$5 [L]
This can be well handled in rewrite rules and it is much faster than doing in PHP also. You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(upload/images)/small/(.+?)-(\d+)x(\d+)\.(jpe?g)$ image-resizer/index.php?image=$1/$2.$5&width=$3&height=$4 [L,QSA,NC]
You should not try to extract information from the path in the .htaccess rewrite rule. It's much easier doing that in PHP. So what I do is to feed the whole SEO optimized URL to the PHP page behind the rewrite rule. My .htaccess looks like this:
RewriteRule ^(.*)$ /page.php?SEOPath=$1 [NC,L,QSA]
You can see how simple the rewrite rule now is. Then in PHP I work on the SEO path, and extract the wanted information, which is not too difficult.
Of course you still need to adapt this to your situation. For instance, restrict it to .jpg files only. In your case it could be:
RewriteRule ^(.*).jpg$ /module/your.php?SEOPath=$1 [L]
Related
What I'm asking for is simply rewriting any URL-given sub-directiories to a PHP URL parameter for nicer looking, user friendly URL.
I know that this is possible using the Apache Rewrite Engine and associated parameters.
The problem on my end is that I do not want to rewrite i.e
mydomain.com/parameter ----------->
mydomain.com/index.php?id=parameter
But rewriting
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
In my root folder I have an .htaccess file for 404 errors and such, redirecting non-existant pages to the main one. During the tests I've done I have deactivated these thinking that they could overwrite the other .htaccess file.
I tried...
RewriteEngine On
RewriteRule ^directory/([^/\.]+)/?$ index.php?id=$1 [L]
...and other snippets similar to that one.
The .htaccess file containing this code was placed in the root/directory folder.
What I wanted to achieve with that was to redirect root/directory/string to root/directory/index.php?id=string.
Since my knowledge of .htaccess and rewriting is obviously limited, I need answers to two questions.
Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?
How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?
This rule should work for you in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(directory)/([^/.]+)/?$ /$1/index.php?id=$2 [L,NC,QSA]
Make sure there is no other .htaccess file in directory.
I have 20 million images spread across 3 subdomains. Here is an example:
subdomain3.domainabc.com/images/image5.jpg
I do not wish to move these images to my new server/domain and I do not wish to link to them directly. Instead, I want to use my htaccess file on domainxyz.com. So, on domainxyz.com when this image is called:
subdomain3.domainxyz.com/images/image5.jpg
It goes to server domainabc.com and displays the correct image. Is this possible? Here is what I've put into domainxyz.com htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.domainxyz\.com$
RewriteRule ^(.*)\.jpg$ http://%1\.domainabc\.com/$1\.jpg [R,L]
Take a look at: Mod-rewrite rule for external pages?
Tunneling is not possible; only redirecting.
I made an website with php and I wrote .htacess file in my htdocs folder:
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)/?$ ?mainquery=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ ?mainquery=$1&subquery=$2 [QSA]
My Intention is to redirect the url:
'/A/B' => '/?mainquery=A&subquery=B'.
If the main query is one, everything works fine.
but when the url has two queries, php works fine, but the folder hierarchy moves, so every images and css files with relative urls doesn't work.
It works fine that
(I'm building my web on MAMP, so the domain is now localhost.)
'localhost/publications/articles' => 'localhost/?mainquery=publications&subquery=articles'
but the html thinks the main url is 'localhost/publications', not just 'localhost/'.
so every img tag which has src attribute like
img src='images/myImage.jpg'
doesn't work, because the html thinks the image is in 'localhost/publications/images/', not 'localhost/images/'.
So does css file.
I'm pulling my hair for 3 days, but I can't solve it myself.
How can I solve this problem?
Change this line:
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ ?mainquery=$1&subquery=$2 [QSA]
To this:
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /?mainquery=$1&subquery=$2 [QSA]
The rewritten rule need to be root-relative, else it's going to be relative to the requested URL, which is in a sub-folder already.
I think that's what you're asking. If you're just asking how to point to images/css/js root-relative, change this:
<img src="images/myImage.jpg" />
To this:
<img src="/images/myImage.jpg" />
i was trying to rewrite my urls in a friendly way; i have written all the php code to handle everything.
The only thing that i am unable do is removing the .php extension while preserving the uri parameter.
So, essentially, i'm looking for some .htaccess rules to change from this:
www.example.com/biography.php/john-doe
to this:
www.example.com/biography/john-doe
(john-doe isn't a real file, it's only the GET parameter passed to biography.php)
Thanks everyone.
If the links on your site are already like this
www.example.com/biography/john-doe
and you want to process them on the server like this
www.example.com/biography.php/john-doe
add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteRule ^([-a-zA-Z]+)/([-a-zA-Z]+)$ $1.php/$2 [L]
Should do:
RewriteRule ([^\.]).php(.*) $1$2 [QSA,R=301]
I am working on building my first search-engine friendly CMS. I know that perhaps one of the biggest keys to having and SEO site is to have search-engine friendly URLs. So having a link like this:
http://www.mysite.com/product/details/page1
will result in much better rankings than one like this:
http://www.mysite.com/index.php?pageID=37
I know that to create URLs like the first one, I have one of two options:
use a web technology, in this case PHP, to create a directory structure
leverage Apache's mod_rewrite add-on to have these URLs passed to a PHP processor
As far as the PHP goes, I'm pretty comfortable with anything. However, I think the first option would be more difficult to maintain.
Could someone show me how to write an .htaccess file, which will:
silently direct SEO URLs to a processor script
not redirect if the requested URL is an actual directory on the server
Is there a better way than the way I am trying it?
You can use .htaccess for apache, create file in your root folder of web mainly "htdocs" name it ".htaccess" add next content to it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Options -Indexes
</IfModule>
in your php file you can access data from $_GET
$_GET['url'];
Then you can use data to parse what you need.
Yes, the first option would be pretty hard to maintain. If you want to change the header of the pages, you'd need to recalculate all of the pages.
The simplest way to do that would be to have a PHP file named product.php or product/details.php and use the $_SERVER\['PATH_INFO'\] variable to figure out what the client requested.