Treat .php File as Root Folder - php

So right now I have a index.php file that acts as an API in the root of a subdomain.
So if you go to http://some.domainname.com/index.php/items/ for example you get a response.
However if you go to http://some.domainname.com/items/ you get a 404 error.
I'm assuming there is some .htaccess rewrite command I can add to make this work but I haven't had any luck finding it.

Here is an example based on how Magento does it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It will effectively remove the index.php from your urls, but it won't stop anyone from accessing a file that actually exists, like www.example.com/i-exist.php
Note:
This is specifically geared toward a site whose urls alway end with a trailing slash (/), like www.example.com/index.php/somepage/. This is set with the RewriteBase / line.

Related

.htaccess redirect index.php only (exclude if variable after)

Long story short, I updated an e-commerce website but had to install the new static CMS system into a sub-directory (/wear). The main e-commerce store is still sitting in the root directory (/), and due to the large amount of products and SEO impact, I need to leave it there.
I would like to setup a requests for just index.php to redirect to /wear/
At the same time, if there is a request for index.php?XXXXX I would like it to still use the index.php file.
I've tried using the following .htaccess code but it's redirecting everything. Can anyone help me with this? I apologize for asking this as I know there are multiple threads, but none seemed to provide a good answer.
Attempt 1
RedirectMatch /index.php https://domain/wear/
Attemp 2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wear/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The issue comes from your RewriteBase. When setting it up to /wear/, RewriteRule ^index\.php$ - [L] actually translates to RewriteRule ^wear/index\.php$ - [L] which is not really what you want.
I would try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /wear/ [L,R=301]
What it does is: check that the query string is empty, and if it is, redirect index.php (at the beginning of the request URI, so /index.php only) to /wear/
You will also need to make sure than mod_rewrite is active.
To do so, you can remove the <IfModule mod_rewrite.c> and </IfModule> parts. If mod_rewrite is not available, it will trigger an Error 500 as the RewriteEngine command will not be recognised.

Ignoring a subfolder with .htaccess

In my public_html I have 2 Folders, wordpress and tickets. I currently have the /wordpress directory working via mod_rewrite so that the URL's look nice. I'd like to setup an instance of OSTicket, and upon navigating to example.com/tickets to start the configuration, I'm hit with a Wordpress 404 error.
My root .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/tickets/.*
RewriteRule ^tickets - [L,NC]
RewriteRule ^index\.php$ /wordpress/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
I also have a .htaccess in the tickets folder, with;
RewriteEngine off
If anyone has some insight or perhaps something I should look into it would be most appreciated, thankyou all kindly.
The condition you have is the opposite of your match. They can't both be right. Drop the condition permanently, and amend the rule to this:
RewriteRule ^tickets(?:$|/) - [L]
That ensures /tickets-foo/ still goes to WordPress and that you're not matching case-insensitively when your filesystem is case-sensitive.
Temporarily rename the tickets dir. Create another one with only an index document in it, content is irrelevant but put something in. Visit example.com/tickets and you should see your aforementioned content.
I'm betting the issue is not in the .htaccess nor WordPress.

.htaccess WordPress ignore directory and remove index.php from that directory

So I know how to tell WordPress how to ignore a directory using the .htaccess file but I want to be able to remove the index.php from inside that directory too so I can access like this:
wordpresssite/directory/controller/function = wordpresssite/directory/index.php/controller/function
I've been trying but I'm clearly not very good at writing .htaccess files.
Thanks
My .htaccess file
The wordpress site is called smartronic and the subdirectory I want to ignore and remove index.php from is /mailin
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /smartronic/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(mailin|mailin/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /smartronic/index.php [L]
</IfModule>
# END WordPress
At the moment going to smartronic/mailin takes me to the xampp root page or a 404 page
To insert index.php into a fixed URL, you can use a simple RewriteRule
RewriteRule ^mailin/controller /mailin/index.php/controller [L]
If controller is variable, you need to capture this part with (...) and append it after the substitution with $1
RewriteRule ^mailin/(.*)$ /mailin/index.php/$1 [L]

mod_rewrite in Wordpress installation to replace multiple keys/variables

I am trying to create mod_rewrite rules to redirect the following url in my wordpress site:
www.mywebsite.com/pg/2/row/20/filter/all/filtercity/foo,bar
to
www.mywebsite.com/detail?pg=2&row=20&filter=all&filtercity=foo,bar
My problem is that wordpress included the following rules in the .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I tried the following RewriteRule and it kind of works if I comment the last RewrireRule from Wordpress.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]]
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$2 [R]
</IfModule>
# END WordPress
The above works only for urls like
www.mywebsite.com/pg/2 => www.mywebsite.com?pg=2
A few questions:
1 - how can I configure .htaccess to process the url only when it finds the following keys
/pg and/or /row and/or /filter and/or /filtercity
for all other urls, it should execute the standard Wordpress RewriteRule . /index.php
2 - also the url can have all of the keys or only a few of them. For example:
/pg/2/row/20
/row/20/filter/all
how can I configure the .htaccess to process all the different combinations of keys?
3 - when I tried replacing
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$2 [R]
with
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ detail?pg=$1 [R]
I got a 404 error from the server. Not sure why. Any ideas?
Thank you.
EDIT 1:
I tried adding [L] to the end of my RewriteRule (see below)
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$1 [R,L]
and moving the standard Wordpress rewrite rule to the next line
RewriteRule . /index.php [L]
After doing that I no longer get server 404 error. However the CSS files stop loading.
1 - how can I configure .htaccess to process the url only when it
finds the following keys
/pg and/or /row and/or /filter and/or /filtercity
Read this Rewrite URL with .htaccess for multiple parameters
The above answer helps you to solve your problem when have a standard url format.
2 - also the url can have all of the keys or only a few of them. For
example:
/pg/2/row/20
/row/20/filter/all
You are trying to do something out of URL semantics. When you try to access a resource, the path should be same every time. You can pass pg/0/ if you are on the starting page. But who can stop you from going out of URL semantics? :) In that case you have to have all the combinations as rules in your .htaccess. Follow the above solution, with all URL combinations it will work.
My Suggestions:
Don't Struggle with .htaccess if you are not familiar and your need is only URL rewrite (it is powerful though)
Pass everything to the index file and parse in your code, for your case have a look at this page http://codex.wordpress.org/Query_Overview

How to add an exception to a mod_rewrite rule (wordpress site)

I have a WordPress website im trying to call a php script that is located on the website the URL is something like this
http://example.com/folder/process.php
The problem is when I try to do this i always get redirected to the themes "not found" page.
This is the htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
If i put a test.html file in that directory it will work, and show the contents of the .html file. I think it has something to do with the .php extension it doesn't like?
If i call example.com/wp-config.php it finds that file and deosnt show a 404 page..
This Rule says "If request starts with index.php, do not rewrite and stop".
RewriteRule ^index\.php$ - [L]
Immediately before or after that line you could add something like
RewriteRule ^process\.php$ - [L]
To not rewrite requests to that particular file.
Note that you will have to put your file next to Wordpress' index.php for this to work properly.
Change your .htaccess to this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/+folder/process\.php$ [NC]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try turning off mod_rewrite altogether in your /folder/ directory by creating an htaccess file in there and simply adding this single line:
RewriteEngine On
It will make it so any rules from the parent directory won't be applied. If the weirdness with PHP continues, you may want to check if there aren't any rules in your vhost/server config or that things like Multiviews are turned off (via Options -Multiviews).
Ok so if I took a default file that came with the WordPress install made a copy of it in the cpanel rename it remove the code and than put my code in it.
The file is found and it works. So im thinking the only thing left it could have been was a permissions problem. The permissions i put for the file when i uploaded it was 777. I know NOTHING about file permissions.
And i have been working on this for 5 hours and im done trying to figure out what the problem was it works..and im good with that.

Categories