Redirect .jpeg files to .php files [duplicate] - php

Hi im trying to do redirect from .jpg file to specific URL address but it don't work. Can sb help me?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^/up/([\w]*\.jpg)$ /file?name=$1[R=301,L]
for eg.: i have image www.mydomain.com/up/image.jpg
and i would like to redirect it under: www.mydomain.com/file?name=image.jpg

Try this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^up/(.*)\.jpg/?$ /file?name=$1.jpg [R=301,L]

RewriteCond is totally redundant here, you can match this pattern in RewriteRule itself. Try this rule:
RewriteEngine On
RewriteBase /
RewriteRule ^up/(.+?\.jpe?g)$ file?name=$1 [NC,QSA,L]
Also you may not need R=301 here since you don't want to expose your internal handling of image to clients.

Related

I need rewrite .htaccess url old to new help me

I've changed my url a wordpress, however I am with some errors.
With some images and post ajax. My new url : http://practicetends.tempsite.ws/ but I have much images with domain is www.practicetends.com.br Url old : www.praticetends.com.br I need redirect posts ajax and images to url new... I try url rewrite but not work for me!!! I tried it but it didn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^practicetends.com.br$
RewriteRule (.*) http://practicetends.tempsite.ws/$1 [R=301,L]
</IfModule>
It won't work. You could make it work, if the domain is pointing to the same server / virtual host. I suggest you do a search and replace of the old URL to the new URL using this https://interconnectit.com/products/search-and-replace-for-wordpress-databases/.
You can do a dry run and see exactly what will get changed.

htaccess: RewriteCond matched what is in tester unmatched

I have another problem about mod_rewrite. If user wants image (.png, .jpe?g, .gif at end of uri) then redirect to index1.html, otherwise index2.html. Tester (http://htaccess.madewithlove.be/) doesn't match uri without .png,jpe?g,.gif as within what is correct, but my localhost does and that is the problem.
Corrent example:
Input:
localhost/image.png
Output
localhost/index1.html
Input:
localhost/image
Output
localhost/index2.html
But what my localhost really do:
Input:
localhost/image.png
Output
localhost/index2.html
Input:
localhost/image
Output
localhost/index2.html
My htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^(.*)(.png|.jpe?g|.gif)$
RewriteRule ^(.*)$ index2.html
RewriteCond %{REQUEST_URI} ^(.*)(.png|.jpe?g|.gif)$
RewriteRule ^(.*)$ index1.html
Can you help? Thanks.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (?!.*?\.png|.*?\.jpe?g|.*?\.gif)^(.*?)$
RewriteRule ^(.*)$ index2.html
RewriteCond %{REQUEST_URI} (?=.*?\.png|.*?\.jpe?g|.*?\.gif)^(.*?)(.png|.jpe?g|.gif)$
RewriteRule ^(.*)$ index1.html
Try this.Added a lookahead.Postive for .png and negative for no .png .
Have it this way:
RewriteEngine On
RewriteBase /
RewriteRule \.(png|jpe?g|gif)$ index1.html [L,NC,R]
RewriteRule !^(index[12]\.html|[^.]+\.(png|jpe?g|gif))$ index2.html [L,NC,R]
You're ridiculously abusing RewriteCond. Simplify it to:
RewriteEngine On
RewriteBase /
RewriteRule (\.png|\.jpe?g|\.gif)$ index2.html [L,R=303]
RewriteRule !^index[12]\.html$ index1.html [L,R=303]
The [L] modifier ('Last') ensures no further rules are processed, eliminating the need for a check in the second rule. The [R=303] makes it a 303 (See Other) redirect - alter the HTTP status code if needed.

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

htaccess add .html extension for urls with or without trailing slash

So to begin with I have a custom url rewrite that sends a request variable to a php script
Rewrite rule is below:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
So if you access something like domain.com/slug-text it sends slug-text to index.php located in folder named test.
What I want is all my urls to look like domain.com/slug-text.html, but slug-test variable should still be sent to index.php file.
And
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text or domain.com/slug-text/ to domain.com/slug-text.html and slug-text sent to index.php file located in test folder.
Searched a lot but could not find the answer for this question anywhere on the Internet.
Thank you all for the help.
UPDATE:
my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/ does not get redirected to domain.com/slug-text.html
domain.com/slug-text works as intended redirecting to domain.com/slug-text.html
What do i need to change?
This rule:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
Will trap domain.com/slug-text, domain.com/slug-text/ and domain.com/slug-text.html and send slug-text to /test/index.php inside slug param.
If you really want to redirect using [R=301] from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
This is supposed to redirect /slug-text to /slug-text.html
RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html
This is in the case when slug-text is only letters, digits, – and _.
Rewrite slug-text.html to a php file and pass the slug as a param:
RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L]
If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request.

passing a query string to URL using .htaccess

I am trying to add the page title into the URL. Just like this site does! Basically I want to rewrite the url from
www.siteurl.co.uk/project-82
to
www.siteurl.co.uk/project/the-title-of-the-project
The string that is passed from the php to html file is {PROJECT_TITLE}. How would I add this in the .htaccess file? Thanks guys!
EDIT:
This is my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1? [R=301]
RewriteEngine On
RewriteRule ^project/([a-z-]+)/?$ index.php?project_title=$1
With the above .htaccess, when accessing
www.siteurl.co.uk/project/the-title-of-the-project
You will in your PHP script have a variable $_GET['project_title'] containing "the-title-of-the-project".

Categories