I'm moving an old site from flat PHP files over to a new WordPress installation and want to make sure all the old URLs redirect properly. For example,
Old url: /va/apply.php
should now go to:
New url: /veterans-affairs/apply
I've got /va redirecting to /veterans-affairs properly, but cannot get the .php stripped from the URL.
I'm not sure if these needs to all be done in one step? I've tried everything I can find online and made as many tweaks as my limited knowledge in .htaccess has allowed.
This is also on WordPress, so there may be something I did that was conflicting with the pretty permalinks stuff there.
This is some of the code that I've tried among many others.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
This should redirect the user to the non-PHP location, but I keep getting a 404. This must be a combination of my code and WordPress' pretty permalinks.
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\.php$ $1 [L,QSA]
I have just had a quick look through where you are at and this above might help out. Add it to the wordpress htaccess above all the entries there so it can change this first... HTH
OK, I've finally got this working correctly. Again, what I'm trying to solve is to get this URL:
/va/apply.php
to correctly redirect to the new WordPress URL,
/veterans-affairs/apply
What worked for me was:
# This will remove the .php extension if it is not a directory, the file does not exist and it's not a WordPress specific admin page
RewriteCond %{REQUEST_URI} !/wp-(content|admin|includes)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# The basic redirect for /va
Redirect /va /veterans-affairs
I think what was breaking it was this final line that you find in all the examples:
RewriteRule ^([^/.]+)$ $1.php [L]
I think this was trying to actually resolve the URL before WordPress could do what it needed to do.
I also found this page which proved insightful
Hide .php Extension, Set Directory Index, Eliminate Duplicate Content, etc.
Related
This morning, I was trying to remove file extensions from a website I am working on. While I am not familiar with .htaccess files, I am trying to learn them and their regex online. The first one that I tried failed to preserve $_POST values, and therefore broke the website. I found this longer block of .htaccess code on here and tried using it, and it fixes the post issue well.
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L,NE]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
However, I have found that while it drops extensions well, when I go to certain files or back from them, my files have their extensions back. This appears to stop when I destroy the session. I hope this does not seem like a trivial or repeated question, but I find it unusual that some files have their extensions hidden while others do not. For example, I will see Site/home, then go to Site/about.php, and when I come back I will be at Site/home.php. Is there something obvious I am missing here? Thank you!
RewriteEngine On
RewriteRule ^index?$ index.php
I figured this out. The .htaccess file works, for anyone trying to do the same thing. My forms were going to Site/about.php, which I assumed would automatically correct to Site/about. I went through my code and removed all file extensions from forms, hrefs were fine. This fixed it, so far no bugs! Thank you all who considered this question.
RewriteEngine On
RewriteRule ^about?$ Site/about.php
OR
RewriteRule ^Site/about?$ Site/about.php
I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I've been trying to figure out how to remove .php extension, I've searched everywhere and it seems most of the code are not working anymore. The code below is what I am using now to remove the .php extension but it is not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- f
RewriteRule ^([^\.]+)$ $1.php [NC]
I'm using XAMPP on Windows 10. I also tried many times to remove the .php extension but the result was the same. But after a long while finally, I got how we can do this. If you want the same results, just follow the steps given below...
To remove the .php extension from a PHP file, e.g. if you want to change mywebsite.com/about.php to mywebsite.com/about you have to add the following code inside your .htaccess file:
First of all, make sure you've created a new file named .htaccess and placed it at your root-level/root-directory where your index file is placed.
Now, open this (.htaccess) file with any editor of your choice, copy/paste the given code and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Now, if you (any user) will access mywebsite.com/about in the browser, the user will see the content of mywebsite.com/about.php page.
But still, if you (any user) will access the URL as mywebsite.com/about.php, this will not redirect the user to this mywebsite.com/about page. But will go to this mywebsite.com/about.php page which means the user can still visit mywebsite.com/about.php page. Don't worry about it. If you want to avoid this, you can simply follow the next step.
To avoid the above problem, now you need to add some more rules in the .htaccess file. For this, you've to replace your old code with this new one given below, save your file and check it out...
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
That's all and you're all set.
I hope this will fix everyone's problem.
Have a nice day :)
To remove the .php extension from a PHP file
for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Or
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
Or
see removing .php extension from URL
Also, make sure that you've mod_rewrite on.
Also see how to create .htacess file
I think you need to change internal website coding as well with htaccess code above and remove the file name URL extension from where it is mentioned in your web coding.
Eg:-
https://yoursite.com/page.html
changed to
https://yoursite.com/page/
htaccess code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
The problem: Some html pages of php equivalents (apple.html, apple.php; orange.html, orange.php), but not all do (grapes.html).
The goal: If the php version exists, rewrite, otherwise keep it the html version.
I have:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ /$1.html [R]
Interesting issues:
If I don't put / in front of $1.php then I end up with: site.com/document/root/path (ie: site.com/home/user/www/file.php)
When calling the second RewriteRule, I get http://site.com/http:/site.com/page.html and it tells me there were too many redirects. Notice how there is only one / in the second http.
I've made some progress, I added RewriteBase / and removed the / before the $1, but I still get the too many redirects error (the web page at site.com/page.html has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer).
It seems like if I just rewrite html -> php -> html I get the same error. So it looks like the logic is working, but that sort of logic isn't allowed. I can't think of any other way I could see if a "php version" of a file exists? The only way I can think of is do something similar to:
RewriteCond ^(.*)\.html$ $1.php -f
RewriteRule ^(.*)\.html$ $1.php [R]
Unfortunately that doesn't quite work (I'm guessing because it has three segments on the condition line). I'm trying to get the filename without the extension, then say if filename.php is a file, rewrite page.html to page.php
you should be able to achieve that by using two conditions:
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [R,L]
The first condition checks if the filename ended with .html and the second uses the back reference %1 from the first condition to check if .php version exists.
Hope it helps. :)
I'm sorry to answer sooooo late but you will need to add the RewriteBase directive to make it works.
I had the same problem (with your http:/stufff) and fixed it this way :
RewriteBase /your/application_path
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [L]
Hope it will help !
I also wanted to Rewrite all .html request to .php files, but only if the .php file exist. But my variation was that this should only happen if the actual .html file does not exist.
So only calls to .html files that does not exist is Rewritten to .php file, if they do exists by these rules: (I also have tested this on a XAMP local server and also a Apache online server with success!)
# Rewrite rules to .html file to .php if the .php version
# does actually exist.
RewriteCond %{REQUEST_FILENAME} (.*)\.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %1\.php -f [NC]
RewriteRule ^(.*).html$ $1.php [NC]
It first check if the requested file is a .html file (we don't want .jpg, .css, .pdf, etc. to be rewritten).
The it checks if that .html file does not exist. (we don't want to rewrite to .php if it actually does exist.)
Then it checks if a .php version does exist (we don't want to rewrite to a non existing .php file).
Then it rewrites the .html to .php
I need help configuring my .htaccess file to handle redirects properly.
Here’s what I need to have happen. Stackoverflow's spam filter wouldn't allow me to post the full domain. So where I say "DOMAIN" you can substitue "domain.com". (I also needed to add and extra t to the http.)
Requests for the DOMAIN/page version of the file should be redirected to www.DOMAIN/page.
Requests for the 'friendly' versions of the URLS should be allowed. So a file that is really at www.DOMAIN/index.php?q=37 should be viewable by going to www.DOMAIN/latest-news
I have a big list of 301 redirects. We recently changed the site from an .asp based CMS to one written in PHP.
Example:
redirect 301 /overview.asp http://www.DOMAIN/overview
Items 1 and 2 are working fine.
However for item 3, if I put in a browser request for "http://www.DOMAIN/overview.asp" instead of redirecting to the friendly name of the file ("http://www.DOMAIN/overview") it will redirect to http://www.DOMAIN/index.php?q=overview.asp. This is the problem.
What do I need to change to get this working right?
My configuration is below:
## Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
## Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets)/*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]
## For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.DOMAIN/$1 [R=301,L]
redirect 301 /overview.asp http://www.DOMAIN/overview
redirect 301 /news.asp http://www.DOMAIN/news
# ETC....
thanks!
Mod_rewrite is doing exactly what you're asking it to do ... (yes :-), that's often the problem with computers).
On the /overview.asp http://www.DOMAIN/overview line you're setting the browser to send out a brand new request from scratch, which starts the whole cycle again from the top and gets catched by the ^(.*)$ index.php?q=$1 directive.
Right before this line you should put another RewriteCond to prevent the ^(.*)$ rule to apply if REQUEST_FILENAME is either overview or news. You might also simply rewrite /overview.asp to overview [L] instead of redirecting.
If you can, set the RewriteLog directive to its highest verbosity and look at the logfile - it usually gives very good insights into what's really going on...
EDIT - if I get it right you shoud be doing this:
RewriteCond %{REQUEST_FILENAME} ! \.asp$
RewriteCond %{REQUEST_FILENAME} ! ^overview$
RewriteCond %{REQUEST_FILENAME} ! ^news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This would prevent any file already ending in .asp, plus those looking for overview and news, to be redirected toward index.php.
I suspect anyway that you got something backwards regarding that SEO stuff. You should indeed start from the structure of the query string that your scripts expect and use that as a base to build a sensible URL addressing schema.
EDIT #2:
There was a space too many between the bang mark ant the regex. The following code doesn't come from memory as the previous - I've tested on my local Apache and it does what's supposed to do (as long as I've understood correctly..)
RewriteCond %{REQUEST_FILENAME} !\.asp$
RewriteCond %{REQUEST_FILENAME} !overview$
RewriteCond %{REQUEST_FILENAME} !news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Hope this helps