Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]
Related
I have a website of PHP files that I'm hosting on SiteGround.com and want the links to look like "example.com/about" instead of "example.com/about.php". I also want my index.php to be loaded when I visit "example.com" instead of "example.com/index"
So I did some searching and this is what I want:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But the webserver already has a default .htaccess file which has this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule . /index.php [L]
</IfModule>
Okay...so I tried to piece together what it is doing. Based on my understanding, it enables the RewriteEngine, then sets RewriteBase to be /. It then sets a rule looking for index.php and if it finds it, it doesn't check any other rule.
Otherwise, it falls through and checks if a string is not a file and not a directory and contains a .php extension, it redirects the root to index.php and stops.
So, I tried adding my three lines to the end of block right before the closing </IfModule> tag and it didn't quite work. Can anyone help me out?
EDIT
I modified my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
In my HTML, I have:
<a href="about" .... ></a> <!-- link to about.php -->
<a href="/" ....></a> <!-- link to index.php -->
Is that right? It doesn't seem to work for me.
First of all, the line:
RewriteCond %{REQUEST_FILENAME}\.php -f
Is not part of the original .htaccess file. It can't be, it makes no sense where it is, perhaps you added it yourself while experimenting.
The .htaccess file you have there, other than that line, is a standard method of putting everything through index.php. Probably for WordPress. So you need to decide what you want to do. Do you want to put everything through index.php, or do you want to use separate .php files with the extension added on the server side? Or do you want to do both? (in which case a combined approach needs writing) Are you still using WordPress? Perhaps you can just remove those rules.
Let me know and I can update the answer if you need more info.
Update
Your rules can be modified to:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,NE]
That should fix it, I think the \. on the REQUEST_FILENAME line was breaking it, the rest are improvements.
If that's not working then perhaps mod_rewrite isn't enabled, or .htaccess files are not enabled. You can test it by putting some garbage on a line and see if you get a 500 error.
If the root doesn't serve index.php then you just need to add a DirectoryIndex.
I would like remove extesion .php and at the same time add /404
I've watched several codes to remove .php, however to remove but adds server paths in the url
I have: http://domain.com/pages/header.php
I want: http://domain.com/pages/header or http://domain.com/pages/header/404
Try this rule.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^header/404$ header.php [L]
put this in pages directory.
I am trying to redirect a bunch of old blog article URLs using the .htaccess file:
RedirectMatch ^/index\.php/global/article/(.*)$ http://www.mywebsite.com/blog/article/$1
This doesn't really work, however, because my CMS seems to get confused by the index.php bit and keeps adding ?symphony-page= to all the URLs.
It's probably this part that is responsible:
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Can anybody help?
Please try the following (comments included to explain):
RewriteEngine On
# First, redirect the old URIs to the new ones via a 301 redirect.
# This will allow the CMS to take over using the rules that follow.
RewriteRule ^index.php/global/article/(.+)$ /blog/article/$1 [L,R=301]
# Frontend Rewrites - for the CMS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Try this:
#-- Input URL >> http://www.mywebsite.com/index.php/global/article/abc
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} ^/index\.php/global/article/(.*)$
RewriteRule ^.*$ blog/article/%1 [R=301, L]
#--Output URL >> http://www.mywebsite.com/blog/article/abc
I've tested the above with this htaccess.madewithlove and it seems like it will work just fine, hopefully it will work for you too
Screenshot:
i got a site php based with some rules in the .htaccess file to get rid of file extentions in the url adress bar. Basically it takes http://netbureau.com.br/en/about.php/ and turns it into http://netbureau.com.br/en/about.
Here are the lines in the htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when i try to access the rss feed of the blog at http://netbureau.com.br/blog/?feed=rss2 and when i try to set custom permalinks for the blog at http://netbureau.com.br/blog. It gets messed up by the htaccess file.
So is there any way to disallow the RewriteRule for the /blog folder so that i can get back my rss link and set custom permalinks in the blog?
I know it's at the same time Wordpress related but it feels more connected to the htaccess file than Wp.
Thanks in advance.
EDIT #1:
I've set up the wordpress permalinks to the default structure which goes like this: http://netbureau.com.br/blog/?p=123 This made my rss link back for good.
The remaining problem is that Wordpress gives me its own rewriterule which is:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Is there a way to to still use the first rule to apply to the whole site except the /blo/ folder and apply the WP rule only to the /blog/ folder?
I've tried different combinations but without luck so far. I could only have the site without the custom links for the blog or the custom blog links and a 404 on the pages of the site.
Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The !^/blog/? expression excludes any URI that starts with /blog
I'm having trouble using .htaccess.
This is the content of my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.php$ $1 [L]
I opened up a text file, pasted these lines and saved it as .htaccess
It was showing .htaccess before i right clicked the .htaccess file and changed it to "open with notepad". I guess that shouldn't make a difference but now its showing a blank name.
The main problem is when i open my localhost on the browser through wamp, the folder where I've kept the .htaccess file, isn't visible or if i access it shows this internal server error.Now, if i remove the .htaccess file from there, it shows up in the localhost directory and doesn't show an error when i try to open it.
If you're getting the 500 Internal Server Error, your .htaccess file is being read. From what I see, you may be missing spaces before the !:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
#-----------------------------^^
RewriteCond %{REQUEST_FILENAME} !-f
#-----------------------------^^
RewriteRule ^([^.]+)\.php$ $1 [L]
But if you want your users not to see the .php extension, the RewriteRule is backwards. The first part should not contain the .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# The input URL has no PHP, but internally is served as .php
RewriteRule ^([^.]+)$ $1.php [L]