change filename with htaccess - .htaccess

We have some folders that includes some files without extension
for example :
/files/a0/ffa5301469e1fb84210e8b5a4a8f3ad3
/files/ff/ff957d05512ea1e71784e811ca45f1f9
/files/f6/f6ffa309ac3fe7fb5b01b07a37bb0d1d
as you see this files have not any extension , i want to add extension and change filename with rewrite rule :
for example :
REAL PATH : /files/a0/ffa5301469e1fb84210e8b5a4a8f3ad3
REWRITE : /files/a0/ffa5301469e1fb84210e8b5a4a8f3ad3/filename.zip
so i need "/files/a0/ffa5301469e1fb84210e8b5a4a8f3ad3" download as "filename.zip"
i tried a lot to edit .htaccess and rewrite rule but i can not succeed
how can i change filename and file extension with rewrite rule ?

You can try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+files/([a-f0-9]{2}/[a-f0-9]+)($|\ |\?)
RewriteRule ^ /files/%1/filename.zip [L,R]
RewriteRule ^files/([a-f0-9]{2}/[a-f0-9]+)/filename.zip$ /files/$1 [L]
in the htaccess file in your document root, but depending on the headers returned by the request, it may be telling the browser what type of attachment and filename it should be using, which means what the URL looks like will have no affect.

Related

Rewrite A Filename To Another Filename Via .htaccess

I wanna change "announcements.php" to "blog.php" in the url. Individual article's are identified by id=x, such as"annoucements.php?id=1", which I will need it to say "blog.php?id=1".
I tried:
RewriteRule ^announcements.php ./blog.php
Try with:
RewriteEngine on
RewriteRule ^announcements\.php /blog.php [NC,L]
If you change the file name in the url, you do a redirection, not a rewrite.

.htaccess - How to Override Url Even If someone Puts the .PHP after the filename

I have created the .htaccess file to rewrite url for .php
Before creating the .htaccess files
Url was like : www.xyz.com/contact.php
After creating the file
Its like : www.xyz.com/contact
But if someone writes in the url
www.xyz.com/contact.php
The .PHP extension is displayed hoe to hide it even if someone enters it manually
the following code will remove .php even if users write it :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

Remove .php extension from nested URL

I have the following url, and I simply want to hide the .php extension.
current url :
localhost/api.php/test/pickup/10000
new url
localhost/api/test/pickup/10000
I've been looking around for an answer, but all I can find is a way to remove the .php extension if the url ends with it.
So I wonder how do you remove the extension if the url is nested?
In the htaccess file in your document root, try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+api\.php([^\ \?]*)
RewriteRule ^ /api/%1 [L,R]
Assuming that when you go to localhost/api/test/pickup/10000 it actually resolves to what you want. Otherwise, you can try turning on multiviews:
Options +Multiviews

Redirect duplicate url in htaccess

I have duplicate urls and i need to do some redirection in htaccess
Examples:
Original Urls are :
www.mywebsite.com/listofgames/pacman.html
www.mywebsite.com/listofgames/nintendo.html
www.mywebsite.com/listofgames/snake.html
I have about 1000 games name
Example of duplicate urls :
www.mywebsite.com/listofgames/1/pacman.html
www.mywebsite.com/listofgames/1521/pacman.html
www.mywebsite.com/listofgames/52154/pacman.html
www.mywebsite.com/listofgames/abc/pacman.html
www.mywebsite.com/listofgames/opq/pacman.html
www.mywebsite.com/listofgames/opq/1/2/35/pacman.html
www.mywebsite.com/listofgames/154/3562/snake.html
www.mywebsite.com/listofgames/2541/snake.html
www.mywebsite.com/listofgames/524/snake.html
www.mywebsite.com/listofgames/absc/gtar/15/nintendo.html
www.mywebsite.com/listofgames/nije/nintendo.html
I need to redirect them to original urls
So
www.mywebsite.com/listofgames/anynumber/mygamesname.html
www.mywebsite.com/listofgames/anyword/mygamesname.html
www.mywebsite.com/listofgames/anyword/anynumber/anyword/mygamesname.html
Must be redirected to
www.mywebsite.com/listofgames/mygamesname.html
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/listofgames/.*/([^\.]+)\.html/? [NC]
RewriteRule .* listofgames/%1.html [R=301,L]
Redirects permanently
http://www.mywebsite.com/listofgames/any/number/of/folders/mygamesname.html
with or without trailing slash.
To:
http://www.mywebsite.com/listofgames/mygamesname.html
Effectively removing the segment path between /listofgames/ folder and the HTML file.
String listofgames and extension html are assumed to be fixed, while mygamesname is assumed to be variable.
The incoming URL structure has to be kept for the rule-set to work: Last string inside the URL must be the HTML file.
For silent mapping, remove R=301 from [R=301,L]

How to rewrite an url without beening displayed the file extension

How i rewrite url, using htaccess , i have tryed like this :
RewriteRule ^file$ file.php [L]
i don't want to be displayed the file extension
i want to rewrite url like this :
http://example.com/file.php into http://example.com/file
RewriteEngine on
RewriteRule ^file$ /file.php

Resources