htaccess mod_rewrite RewriteRule - .htaccess

I'm having difficulties to get this to work. Maybe someone can help me.
If a user opens
domain.com/folder/images/1.png
the server should rewrite to
domain.com/folder/images/image.php?file=1.png
since some rewrites are required elswhere on the site, I thought I should put the .htaccess file for this one into
domain.com/folder/images/
containing
RewriteEngine on
RewriteRule ^([\.\-_0-9A-Za-z]+)$ image.php?file=$1 [L]
but this somehow sets
$1=image.php
instead of using the actual filename. If I use
RewriteEngine on
RewriteRule ^([\-_0-9A-Za-z]+)$ image.php?file=$1 [L]
the redirect only works for strings without a period in it
If i can get the behavior for multiple folders with one single .htacces file in the root of domain.com it would be even better. Oh and btw, the file extension can only be jpg or png. Can i include that into the regular expression?
Help would be greatly appreciated!

You can put this in your root htaccess (or better in you vhost conf file and disable AllowOverride, to disable the use of htaccess)
RewriteRule ^folder/images/([\.\-_0-9a-z]+)$ folder/images/image.php?file=$1 [L,N]
N for case insensitive url.

Related

Change of address via .htaccess

I am trying to create the first .htaccess file.
RewriteEngine on
#Rewrite for index.php?page=xxxx
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) index.php?page=$1 [NC, L]
I am learning PHP and would like to change the website address view from example.com/test/1/index.php?page=mysql to example.com/test/1/index/page/mysql.
When I pasted the above code to .htaccess file , i get a info that the page cannot be found. When I delete a .htaccess file, the page displays correctly. Is there something wrong with my code in the .htaccess file?
I know index.php?page=abc addresses are dangerous. What is the best way to protect or change the appearance of links in php, is the change in the file sufficient?
Thanks for all the advice
Finally, i found a solution to my problem.
I will share it, maybe it will be useful to someone.
RewriteEngine On
RewriteBase /test/1/
#Rewrite for index.php?page=xxxx
RewriteRule ^([0-9a-zA-Z]+)/?$ index.php?page=$1 [NC,L]
I added the command: RewriteBase - where we write the path to the place on the server where the files are located

301 Redirect .mp4 file to a link

I have a link to a video file (example.com/abc.mp4), which, when clicked, I would like to redirect to another link. We don't want to change the link on the page since this link was given out, so it would make more sense to redirect it, if possible.
I would think there should be a way to do this via .htaccess and RegEx, but have not been able to have any luck finding a solution, or talk about something like this.
Does anyone have any ideas?
There are two chances: either you're doing the redirection rule wrong, or your Apache is ignoring the .htaccess directives. ¿Do you know if another rewrite rules are working? ¿Can you check if your vhost if configured with AllowOverride All?
Additionally, you could try the following rule to redirect a file which doesn't exist. Just to be sure:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^xyz\.mp4 http://www.newsite.com/newfile.mp4 [r=301,L]
</IfModule>

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

.htaccess redirect when URL contains parameters

Yes, I searched the site and found many examples, but my .htacces skills are very limited, so NONE of examples worked for me after trying to modify them to suit my needs.
Put simply, if URL is like "index.php?/............" user should be redirect to example.com (ideally, to example.com instead of example.com/index.php) immediately.
I need this because I have an old domain that was used for forum, so I get thousands of requests like "example.com/index.php?/whatever" and all these virtual pages display the same content, which hurts my rankings badly.
My index.php file has NO parameters at all (but some other files have, so the rule shouldn't affect other files).
Thanks a lot!
Try adding these rules to the htaccess file in your document root (preferably before any other rules you have there)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/
RewriteRule ^index\.php$ /? [L,R=301]

Default language for .htaccess

I believe what I'm trying to do is simple but I've been at it for hours and can't find the solution.
Basically I have a site that has 2 languages: English and French.
I would like that if the url has no language, it redirects to a page,
but if a language is specified in the url, redirect to that page.
For example
www.mysite.com -> goes to index.php?lg=en&page=$1
www.mysite.com/fr goes to index.php?lg=fr&page=$1
In my htaccess file I have this
RewriteEngine On
RewriteBase /
RewriteRule ^fr(.*)$ index.php?lg=fr&page=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?lg=en&page=$1 [L,QSA]
This isn't working. Any help would be much appreciated!
Thanks
EDIT : The code I have supplied is kind of working... the only thing is that it doesn't load my CSS/JS anymore.. when I try to type the whole path of the CSS/JS file, it returns a page not found... I know the path is good because if I comment the first rewriteRule the files get loaded... I have no clue why this is happening.
Is RewriteEngine directive set to On in your Apache configuration? If not, the first line in your .htaccess should turn it on explicitly(RewriteEngine On should be the first line in the .htaccess).
Also you don't really need the second rewrite rule, do you? If no lg parameter is supplied via GET, you can default it to en in your index.php script.

Resources