Rewriting extension PHP to XML using htaccess - php

I have a sitemap in PHP sitemap-latest.php and i want to change the extension to .xml without affecting the file and content in it.
RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sitemap-latest.xml$ sitemap-latest.php [L] #NoEffect
RewriteRule ^sitemap-latest\.php$ sitemap-latest.xml [R=301,L] #Redirect to folders https://www.example.com/var/www/vhosts/mirrorblogs.com/website1/sitemap-latest.xml
RewriteRule ^sitemap-latest.php$ sitemap-latest.xml [L] #404 error for both php and xml
But nothing is happening. example.com/sitemap-latest.php not changing to example.com/sitemap-latest.xml

Related

Folder mismatch after removing file extension .htaccess

I have .htaccess file helping me to remove file extension from my pages, which i should say its working perfectly, but am having problems accessing site folders(domain.com/folder_name/) as am getting a 404 error with name of folder(domain.com/folder_name.php) having an extension in the error message. Below is my .htaccess code
RewriteEngine On
RedirectMatch permanent ^/detail/((css|js)/.*) /$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I finally figured it out; all i had to do was to create another .htaccess in the folder affected then add the following line of code:
RewriteEngine Off
All is solved

.htaccess exclude images from rewrite condition

I've been trying to exclude png and jpg files from being excluded from the rewrite condition. Currently, I have a set of pictures that are being presented as broken images. What is causing the error is a rewrite rule in my .htaccess. That rule overwrites the URL of a folder so it will appear with a different name in the URL, instead of /volley it appears as JeepVolleyballChampionship. I've tried to get rid off the last part that redirects to the home page if a directory doesn't exist, but nothing seems to work. I've also tried to include an exclusion of files with extensions of png and jpg from the rewrite block that rewrites the directory...
What I assume is that I'm not implementing that exclusion correctly. My page still loads fine, except with the broken images. Can you provide insight of what I'm doing wrong in this block?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
My whole .htaccess is provided here:
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
DirectorySlash Off
RewriteEngine on
#try to make it so that /volley/ is eliminated from folders and name changes but it is still under this structure
RewriteRule ^volley$ /volley/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/$ /volley [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/index.php$ /volley [R=301,L]
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
#non-existent folder to default root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
jpg and jpeg are treated as separate extensions here. Add jpeg as well on the line no 3. It may fix your issue, if your extension is "jpeg".
It will look as follows:
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]
You should not be entering "RewriteEngine on" multiple times, and you also need to be careful about the order of your rules. Off the top of my head I think all you should need is this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/volley /JeepVolleyballChampionship [R=301]
The !-f condition will prevent the images from being rewritten (along with all other files) so long as they exist, this is the canonical way to exclude all existing files like js, css, etc.
Then the pattern in the rule will only rewrite URLs that start with /volley, so you do not need a condition to match it -- in fact, the rule pattern is checked BEFORE the conditions, so matching there is faster than matching everything and then checking the conditions.

.HTACCESS not to redirect a specific file

I have a web structure where all my files have extension .php and I am using .htaccess to remove .php from the url.
So the file is www.abc.com/ab.php ----> www.abc.com/ab
This works fine, However, I have few files that are requested in form and are manipulated using JS (this isn't ajax) and because of .htaccess it doesnt work.
Can any one let me know how can I exclude "formfile.php" file from not getting redirected to "formfile"
Here is my code :-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
Add this line of code: RewriteRule ^ajax.php.*$ - [PT]

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

Rewrite .html to .php using mod_rewrite

I have a rewrite condition and rule which is.
RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]
I need to replace .html with .php in %{DOCUMENT_ROOT}$0.
The reason is, I am rewriting my url's to .html but when this file checks for an existing file it fails due to %{DOCUMENT_ROOT}$0 looking for file thefile.html,
I need it to look for thefile.php.
For rewriting .html extensions to existing .php files with the same name, try this rule instead of what you had:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php?%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]
What you have just rewrites any non-existent file to index.php
Solved!
RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^(.*).html$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]
I had to use it twice to firstly, rewite all .html to .php then on the request for.php the second condition comes into play.

Categories