Redirecting php URLs to non-php URLs but with one exception - apache2

Using the code below, I removed .php extensions from URLs and redirected php URLs to non-php URLs. Now I am trying to make it work with an exception. For example, I don’t want a particular page redirected. Let’s say I have a file name “file2022.php” and I don’t want it to be redirected at all. How can I do that?
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule (.+) /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)/?$ /$1.php [L]

Related

.htaccess 500 internal server error when routing api

I'm currently running php on an apache server locally, with a React frontend.
This is how my current .htaccess is laid out:
Options -MultiViews
RewriteEngine On
RewriteRule ^api/(.*)$ api/$1\.php [L]
RewriteCond %{REQUEST_URI} !^/api.*?
RewriteRule ^ index.html [QSA,L]
The bottom condition is so that routing works in my React app. I'm then taking the production build and copying it into my htdocs.
The routing works, however, I want to be able to call the .php files inside my /api directory without using the file extension. So I want anything that comes after /api/ to be redirected to whatever is entered, followed by .php.
E.g. /api/authentication would go to /api/authentication.php, and /api/register would go to /api/register.php, and so on.
With this current setup, I'm getting a 500 internal server error when making requests to /api/authentication etc.
Is there something wrong with my .htaccess file?
Your first rule is looping as you're matching .*. You may use:
Options -MultiViews
RewriteEngine On
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteRule ^api/(.+)$ api/$1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^api index.html [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC] will skip rewriting when a URI ends with .php.

My css file is requiring /index.php as part of the url

Ive taken a copy of expressionengine from our production server to a dev server and its installed and running. I can access the admin console fine and Ive updated my paths.
The problem is that when I try and view my site the main page loads but no CSS is downloaded. The error Im getting is
The requested URL /presentation/layout was not found on this server.
I cant access the file from http://localhost/presentation/layout but I can access from
http://localhost/index.php/presentation/layout
Ive updated my .htaccess file from the following guide
https://docs.expressionengine.com/latest/urls/remove_index.php.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I can access the site now from http://localhost without the index.php but its still looking for it for the CSS file. Any idea how I can resolve this?
First thing: don't put your css file in a template unless you absolutely have to.
You only need dynamical css files if the styles are depending on the data in a channel like
{exp:channel:entries}
.{title} {
color: #{color_custom_field};
}
{/exp:channel:entries}
If you put it in a template it will be processed by the template parser on every page view. This will add extra queries/processing which isn't needed for a static file.
What version of EE are you on?
All of us EE guys are over at https://expressionengine.stackexchange.com/ by the way

Refreshing page shows file not found error

I am using apache server for an angularJS web-app. Its purely based on AngularJS/HTML5 and has no server side programming like php or others.
When I say localhost on my browser (Chrome 49+), it takes me to http://localhost/Login/Index as per the routes defined. But when I refresh the page, browser says The requested URL /Login/Index was not found on this server.
I went through some posts on stackoverflow and tried this in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
</IfModule>
I tried removing few lines and it shows me some error while refreshing the page. So this means that .htaccess file is being applied. So I suppose the problem must be with the RewriteRule. What should I write in the RewriteRule? Any other quick/better way to solve this problem?

.htaccess - Redirect everything to homepage except one directory

I'm creating an AngularJS app and want to avoid the # in the URL. I learned that I needed to add the following .htaccess rules to make it work:
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
That worked great. However, now my calls to my web services are not working. The web service files are inside an /api/ directory, e.g. http://example.com/api/. My guess is that when the api calls try to access those files, they also get redirected and break. So how can I modify the rules above to redirect everything to the homepage, except links that are going to any file inside the api folder? I tried to find an existing answer to this, but while there were many similar ones, none of them was exactly what I needed. Thanks!
I found the code I needed:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /
That's all you need. Got it from: https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

How to make mod_rewrite silently redirect and pass parameter to index when the {REQUEST_FILENAME} is an existing directory or file

Synopsis:
localhost/root/admin is an existing folder
I want /root/admin to go to /root/index.php?url=admin without changing the url in the address bar.
The only except is if the requested file is an image
This is what I have written for the .htaccess file in localhost/root to try and do all this:
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [QSA,L]
What this does instead is redirect localhost/root/admin to localhost/root/index.php and changes the url in the address bar to localhost/root/admin/?url=admin.
It will only not do this if I add a trailing slash (/) onto "admin".
I know that, on my mac laptop, this mod_rewrite code does exactly what it is meant to, but only when running it in the "Sites" folder and not in the localhost htdocs folder.
Any thoughts on what I can do to fix this?
I think you're just missing the "PT" flag to prevent an actual redirect. PT (Passthrough) means that Apache redirects the resource served internally, but the browser sees the same URL. (See the Apache wiki for more details: http://wiki.apache.org/httpd/RewriteFlags/PT)
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [PT,QSA,L]
Note the only change was to add the flag. I tested this on my server and saw the behavior that I think you want.

Resources