So, I'm currently writing my .htaccess file, and I've stumbled upon the issue of root folders. Currently, http://localhost/public_html/index is the root of my website. What I want to do is have the url as http://localhost/index instead.
I know this is pretty simple, but all of the posts I have looked at so far have not proven to work. Here is what I have so far, which is producing a 404 error.
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteRule ^(.*)$ /public_html/$1 [QSA]
Please include any documentation or guides to .htaccess itself if possible.
Thanks.
The following .htaccess code can be added to achieve the results wanted:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public_html/
RewriteRule (.*) /public_html/$1 [L]
Related
I have no understanding at all on how to make/modify an htaccess rewrite rule, and I havent found anything to this specific issue.
I need to change
site.com/file.php
to
site.com/folder/file
So the .php also has to go. It needs to work for all files
Come on, there are plenty of examples for that. A little research and effort would be better.
Anyway, here is how your htaccess (in root folder) should look like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/([^/]+)\.php(?:\s|\?) [NC]
RewriteRule ^ /folder/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder/([^/]+)$ /$1.php [L]
I am editing a website for a client and have hit a bit of a roadblock. I can edit and publish existing pages just fine, but if I were to create a new .php or .html page and try to view it online, I get sent to the 404 error page. I can take the new page I want and rename it to overwrite an existing page and everything loads fine.
I have tried some different ways to do the rules I need, but they either try to find the page in the root (When it's actually in /application/views/); or it works and I have the same problem as before.
I need help making a new .htaccess file that allows you to go to website.com/signup and it takes you to /application/views/signup.php. Below are some examples of what I've tried so far.
Original .htaccess:
RewriteEngine on
RewriteCond $1
!^(index\.php|images|css|js|uploads|db|fonts|blogger|support|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Another version that has the same issue.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php/$1 [L]
Lastly, a version that allows new pages to be viewed but looks for them in the root folder. Eg. The requested URL /signup.php was not found on this server. I imagine if this is modified it will work, but I have no experience with .htaccess rules. Much appreciated to anyone that can help me and future browsers!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
You should be able to add a couple of conditions (RewriteCond directives) to the original .htaccess file rule:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|uploads|db|fonts|blogger|support|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
So, only if the request does not map to an actual file or directory will it be rewritten (to the front controller).
I'm aware of a lot of solutions around there, and tried everything. The problem is, when I put a .htaccess in the codeigniter subfolder, it's not read. I thinks it's because of the AllowOverride directive, but I can't change that.
I tried to skip the main subfolder that is not part of wordpress, one I called "sandbox", in the root htaccess (that's part of wordpress), so it doesn't "inherit" any problem from that rewrite (for example, to not have 404 managed by wordpress in the codeigniter project).
The problem is, as I said, that the .htaccess in the /sandbox/ciproject/ folder is not read. So, I tried this in the main .htaccess, and it worked... for a little time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?sandbox/
RewriteRule . /index.php [L]
RewriteBase /sandbox/ciproject/
RewriteCond $1 ^(application|system|private|logs)
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|img|fonts)
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
That allowed me to make it work for a while... but it's not working anymore. Something more I could try? I already changed the config of the codeigniter project so it doesn't use index.php, but nothing. I only get a server error 500 now, and if I delete the second half of the htaccess, I only get 404 as a response for getting rid of the index.php
I don't know if I'm losing some data, but just ask and I'll edit this, so you're not blind at this one.
Based on my understanding, your code-ignitor part doesn't work because, you have two clashing rewrite rules in your htaccess.
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
These are a clashing rule set. Remove one of the lines and then try. It should work.
Use only this code in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandbox/ciproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
Brief
Hey guys, I will qualify this post by saying I'm an .htaccess beginner and am hacking my way through this at the moment. I'm hoping this will yield a solution but also be a learning experience.
Issue
Currently, I'm having trouble removing the .php file extensions from URLs. Previously, all pages were HTML and I had successfully rewritten URLs to drop the .html extensions, but it just doesn't seem to be working for .php — I'm just getting 404 errors.
.htaccess Contents
ErrorDocument 404 http://www.example.com/404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^{.*}$ $1.php [L,QSA]
Any education/advise you can provide would be greatly appreciated. Thanks!
What I'm ultimately looking to do is do /about instead of /main.php?p=about. I've seen a few posts (like this!) but none seem to work from me.
Firstly, I was putting the ReWrite rules in httpd.conf..is this correct?
Lots of places mention .htaccess but I didn't have that file in any directory that seems important (the only references are in phpMyAdmin). I copied the file from phpMyAdmin into htdocs and then replaced the text with a simple rule:
RewriteEngine on
RewriteRule home main.php [L]
However localhost/home reported a Bad Request.
So firstly which file should I use & if .htaccess where should I put the file & do I have to reference it in any other files? Secondly, is the following likely to work for my desired approach?
RewriteEngine on
RewriteRule ^([^/]+)/? main.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Thanks for any clarity & help.
Mike
You actually have conditions (RewriteCond) after RewriteRule. It should be other way round.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /main.php?p=$1 [L,QSA]
RewriteRule ^home/?$ main.php [L,NC]
Suggested Read: Apache mod_rewrite Introduction