htaccess rewrite rule to redirect .mp3 extension - php

I am trying to make a rewrite rule that will rewrite this:
example.com/audio.mp3
To this:
example.com/stats.php?file=audio.mp3
I have:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ file.php?file=$1 [QSA]
This is working as long as I don't add a file extension to the URL. As soon as I do, I get a 404 not found page.
What's wrong?

If you put this above your other lines of code, I do believe it should work:
RewriteRule ^(.*)\.mp3$ /stats.php?file=$1 [L]

Related

Rewriting URL in CodeIgniter

I want to rewrite URL using .htaccess in codeigniter but it's not working. Can you please figure it out. Want to change URL from:
www.site.com/mauritius_holiday_rentals/apartment/anyname
to
www.site.com/mauritius_holiday_rentals/anyname
My current .htaccess file contains:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^mauritius_holiday_rentals/([A-Za-z0-9-]+)/?$ mauritius_holiday_rentals/apartment/$1 [L,QSA]
First four line is for removing index.php from URL which is working fine.
If routes file is set to access new url and you want to set redirection for old URLs then Use following code in .htaccess. otherwise let me know in detail what you want to do.
RewriteEngine on
RewriteBase /
RewriteRule /mauritius_holiday_rentals/apartment/$1 /mauritius_holiday_rentals/$1 [R=301,L]
Routes.php config file code
$route['mauritius_holiday_rentals/(:any)']="mauritius_holiday_rentals/apartment/$1";
Let me know if any problem.
RewriteEngine On
RewriteBase /yourProjectName/
RewriteCond $1 !^(images|stylesheets|javascript)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourProjectName/index.php?/$1 [L]
Try to use codeigniter routing system
https://www.codeigniter.com/user_guide/general/routing.html

How to change weird PHP URL with htaccess code?

Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.

Issue with htaccess

I made a PHP framework that has a built in TPL system. It fetches the ending of the url, and finds the file that has that string. So if the url is http://website.com/tagHere the Tpl system would look for the file called tagHere.php. Everything works fine with it, but I'm now trying to expand the framework to have a built-in backend panel. I've tried multiple ways, but it just refuses to work.
I decided to take a look at Wordpress' htaccess, since they do exactly what I'm trying to accomplish. Here is how their htaccess works.
They check if the requested file name is a file. If it isn't a file, then they check if it is a folder. If it isn't a folder, they redirect to index.php. I want it to check if it's a file/folder, and if it isn't to remove the .php tag from the URL, so the TPL system can get the specified file and return it.
Here was my original .htaccess file.
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1`
Here is Wordpress' original .htaccess file.
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
Here was my attempt:
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1
</ifmodule>
When I try the above htaccess, it returns a 404 error.
The Rule
RewriteRule . /index.php [L]
Will always match every URL because it says something like "if there is a character in the URL, then redirect".
What you're looking for is something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*?)$ admin.php?location=$1 [L]
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1

Error in url rewrite if i remove .html from url

I am having some issues with url rewriting
I am using this as my code for rewrite in htaccess
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /pages.php?id=$1 [L]
this was my url
studentsassignmenthelp.com/pages.php?id=Marketing-Assignment-Help
after using the url rewrite i am getting this
studentsassignmenthelp.com/Marketing-Assignment-Help.html
but i need it without the html like the below one
studentsassignmenthelp.com/Marketing-Assignment-Help
if i use the below code to remove the .html than it shows error 500 server...
RewriteRule ^([^/]*)$ /pages.php?id=$1 [L]
I saw some example but after using them it shows 404 or 500 error
Try this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !pages.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages.php?id=$1 [QSA,L]
try this:
RewriteEngine On
RewriteRule ^([^/]*)$ /pages.php?id=$1 [L]
redirectMatch 301 ^(.*).html $1
This is quite usefull to remove any url extension and avoid broken links.

htaccess to php with different name/variable combos

I have php that works awesome
But I wanna change it from showing variables to look like dirs
eg:
example.com/?mode=page&page=15 to example.com/page/15
and
example.com/?mode=pic&picid=136 to example.com/pic/136
however I only know of one way to do it, such as:
RewriteRule ^(.*)$ index.php?mode=$1&page [R]
but that only works for one case, otherwise it 404s
I tried using RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] so that the whole path gets passed. However, this way it doesn't seem to pass the CSS in my pages.
I'm extremely confused...
Here's my htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is you are rewriting ALL urls to
index.php?url=
So your CSS files also get "redirected" to "index.php?url=" eg
index.php?url=css/styles.css
So you need to add "conditions" to your .htaccess so that certain files (or directories) dont get "redirected". You can do this by using "RewriteCond" (conditions) before your "RewriteRule ".
For example to not "redirect" the css file "css/styles.css" you could do
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Categories