Removed .php extension but now it says page does not exist - php

I was able to get the .php off my files but now when I go to the pages they do not exist(and it's forcing a WordPress 404 page when the target files are actually on the server outside of WordPress). I think the problem might be that it's on a server with WordPress and these files lie outside of word press. There's already rewrite rules for WordPress and maybe it's conflicting with my rewrite rules for other files?
.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
# Remove .php extension on files outside of wordPress
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
UPDATE - Full .htaccess
# BEGIN WP Hide & Security Enhancer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#WriteCheckString:1548706048_68906
RewriteRule .* - [E=HTTP_MOD_REWRITE:On]
RewriteRule ^site(.*) /wp-login.php$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-login.php /index.php?wph-throw-404 [L]
RewriteCond %{REQUEST_URI} /enter$
RewriteRule ^(.*)$ /enter/ [R=301,L]
RewriteRule ^enter(.*) /wp-admin$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-admin(.+) /index.php?wph-throw-404 [L]
</IfModule>
# END WP Hide & Security Enhancer
# 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

You need to do something like this
#remove all PHP extensions from the url
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301] #end and redirect
# Force trailing slash ( probably no longer needed, because `/$1/` )
#RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule .*[^/]$ $0/ [L,R=301]
#add .php back in and continue.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php #no last flag as we can now continue to WP with our nice php extensions back on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm assuming those rewrite rules are correct. I would have to look them up I haven't worked heavily with Rewrites in a few years. Basically
first remove any php extensions and redirect (if they exist)
on a redirect (or a request without .php) add back the php extension to those urls where it was removed so you may need %{REQUEST_FILENAME} !-d just to be safe.
carry on as normal
Just as a general rule of thumb, it's best to do redirects first. That way you can get to the redirect with the shortest amount of work. A redirect is like changing the url and then putting that in the browser and running it again (ie. a brand new request).
Another way to do this is to redirect all requests to a certain folder to an index.php of your making that has a router.
Such as this one (that I made as an example)
https://github.com/ArtisticPhoenix/MISC/tree/master/Router
Then your URL's can be like this
http://yourdomain.com/somedir/index.php/controller/method/...args
#or without the index.php
http://yourdomain.com/somedir/controller/method/...args
Then in HTACCESS all you would do is remove index.php
#remove index.php and redirect
RewriteRule somedir/index.php/(.+)/?$ /somedir/$1/ [L,R=301] #end and
#add index.php back in
RewriteRule somedir/(.+)/?$ /somedir/index.php/$1/ [L]
//wordpress stuff
The nice thing about routers is you never have to touch Htaccess, and with a folder name to key in on it should make things simpler.
Using the router and the example files:
http://yourdomain.com/somedir/user/login
Should go to Controllers/user.php method login.

Related

why is .htaccess not working on live site?

I'm needing to rewrite urls to be "pretty" instead of queries: they need to be /kw/blah instead of kw.php?kw=blah. I've tested the code several times with online checkers, and they say it works/is syntactically correct.
The two sites I used to check are: https://htaccess.madewithlove.be (url testing was with /kw/melbourne-web-design and http://www.htaccesscheck.com
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} blueshiftwebservices\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blueshiftwebservices.com/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]
</IfModule>
# END WordPress
I'm getting an error of 404 Page not found only with the pretty url, not the url that contains the query.
The live site definitely refers to the .htaccess file, so I'm lost at what to do?
The rule RewriteRule . /index.php [L] matches all URLs. So you must insert the rule RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L] before it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect to https
RewriteCond %{HTTP_HOST} blueshiftwebservices\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blueshiftwebservices.com/$1 [R,L]
# are you sure you need it?
RewriteRule ^index\.php$ - [L]
# make it "pretty"
RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]
# process the rest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Show correct URL without extension & redirect if URL is incorrect

I've added a bounty of 50 to this question. I'm trying to make it so that:
all website pages show www.example.com/page/ - instead of /page. My current code below is only successful for www.example.com/page
How can the .htaccess code be corrected to achieve this?
Also, changing the canonical URL meta tags from www.mysite.com/page to www.example.com/page/ - will www.example.com/page/ show up in search engines instead of /page ?
The current .htaccess code is:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^ example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Note that the first rewriting conditions just forward to www. and https, so the only part that might need editing might just be:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
So all I want to do it simply add a '/'. A wordpress blog (presumably PHP files rather than HTML) that achieves my wants on the same website (it's at www.example.com/blog/) has htaccess code being:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I'm not sure how this works but I'd love to learn more about .htaccess and redirects, but nothing much seems to be on the web for beginners. I've also read this post. It also must be https (not sure why but blog posts also can have a http link, whereas other pages redirect to https).
The website also has a blog (php files) on it with its own htaccess file, installed under example.com/blog/ - the htaccess code is the default wordpress one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
Have your .htaccess as this in site root:
RewriteEngine On
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# force https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# remove .php or .html extensions
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(?:html|php)[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# add .html extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
# add .html extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
For issues with relative paths for css/js/images add this just below <head> section of your page's HTML:
<base href="/" />
This works for me, the top one is excluding .php from the URL and the bottom one excludes .html from the URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Using htaccess to redirect user profiles

I am creating a social networking site. Using .htaccess I have successfully removed .php extensions from urls but I am not able to redirect domain.com/u.php?username=[username] to domain.com/u/[username]. Following is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
I have seen many examples and codes in stackoverflow but all of them redirect from domain.com/user.php?username=[username] to domain.com/[username]. But this would affect my website since I have removed .php extensions and I might end up with name clash. Thanks in advance.
You can put this code in your root htaccess
DirectoryIndex home.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# redirect /u.php?username=XXX to /u/XXX
RewriteCond %{THE_REQUEST} \s/u\.php\?username=([^&\s]+)\s [NC]
RewriteRule ^ u/%1? [R=301,L]
# hide php extension (if file exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\?|\s) [NC]
RewriteRule ^ %1? [R=301,L]
# internally rewrite /u/XXX to /u.php?username=XXX
RewriteRule ^u/([^/]+)$ u.php?username=$1 [L]
# internally rewrite (if it exists) extensionless to php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1.php [L]

mod_rewrite if not a certain page

I have a .htaccess file with the typical wordpress mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
However I need to allow a certain page to not get redirected. that being 'domain/ee'
I tried adding:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}%{REQUEST_URI} [L]
to the top. As I read it, that should have checked to see if the page existed and if so not run the reset of the rewrite. However it's not working.
any ideas? thanks.
Make the first rewrite rule something like this:
RewriteRule ^(path/to/page)$ $1 [L]
These two rules should prevent rewriting any actual files or directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

htaccess add trailing slash and force www with clean urls

I'm using my htaccess file with mod_rewrite to create clean urls like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
I would also like to force the site to have the 'www' subdomain and most importunately add a trailing slash if the url doesn't have one.
I am an absolute noob with mod_rewrite and I've tried accomplishing this on my own by combining other code I found on google (sad I know), but I always end up with a 500 error.
Here's the code I found for force www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>
Thanks for your help.
Try separating out the www and the trailing slash check. This is tested and hopefully working for you. You didn't say if you're running placing at domain root or in a subdirectory - usually good info when asking for help with htaccess.
RewriteEngine On
# Assuming you're running at domain root. Change to working directory if needed.
RewriteBase /
#
# www check
# If you're running in a subdirectory, then you'll need to add that in
# to the redirected url (http://www.mydomain.com/subdirectory/$1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# Finally, forward everything to your front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
To debug, comment out the individual sections and see what is/isn't working.
Use this and forgot your problems ;)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*?)/*$ http://%1/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://your-domain.ru/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Categories