htaccess rewrite ".../pages/about.php" to ".../about" - php

I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?

Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.

Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).

Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.

Related

User-Friendly Redirect If File Not Exist

I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...

.htaccess default directory behaviour

I have just started learning about .htaccess files for Apache, I have a website set up so that all requests should come through my index file (which is called Main.php).
"Webpages" are then acquired through a wp GET var (such as wp=forum) - i wish to make this instead Domain/directory/Forum instead of the current Doman/directory/Main.php?wp=Forum
The problem i am facing is that all my "webpages" are stored in their own directory and are made up of "webparts" so forum will be a sub directory of "forum" with files inside it that make up the page. This is causing problems with my redirecting.
I have created the following .htacess file:
#turn redirect engine on
RewriteEngine On
#make sure URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^?]*)$ Main.php?wp=$1 [NC,L,QSA]
This works fine on the redirecting until it is a directory that is entered as the "wp" at which time it will add a trailing slash and corrupt the wp GET var passed (forum/ would be passed not forum as needed)
Here are 2 examples of how I think it may be working:
Main (not a file or directory)
loop 1: Hits the rewrite changes to Main.php?wp=Main
loop 2: URI is file (no change)
MainContent (is a directory)
loop 1: Hits the default directory change? (guess) Changes to
MainContent/ (note the trailing slash)
loop 2: Starts .htaccess and changes this to Main.php?wp=MainContent/
loop 3: URI is file (no change)
Moreover, whenever a file is accessed in my websever with a trailing slash after to (so example: Main.php/) it will display with no links or included files. It will just show the file being requested and seems to ignore any and all css ... php includes or anything else (is this default Apache settings?).
Sorry for the long and possible confusing post. If I need to clean anything up just shout.
Try with that:
#turn redirect engine on
RewriteEngine On
DirectorySlash Off
#make sure URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ Main.php?wp=$1 [NC,L,QSA]
It might be helpful to look at the .htaccess file for Drupal, which does the same thing - i.e. redirect all requests through the index file. The most relevant snippet from that file is:
# Pass all requests not referring directly to files in the filesystem to index.php?page=[rest of url]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The second condition above "! -d" might solve the problem you describe but you might still need to do some "cleaning" in your php code. It's worth looking at the whole Drupal .htaccess file as it does other things to tighten security, such as blocking access to certain files etc.

add trailing slash to my urls

I have a serious problem since two days trying to rewrite the urls of my php website with htaccess.
Options +FollowSymlinks
RewriteEngine On
RewriteRule devis demande-devis.php
RewriteRule mentions-legales mentions-legales.php
RewriteRule condition-utilisation condition-utilisation.php
RewriteRule condition-generales-ventes condition-generales-ventes.php
RewriteRule fournisseur fournisseur.php
RewriteRule qui-sommes-nous qui-sommes-nous.php
RewriteRule faq faq.php
RewriteRule services services.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
But what I expect for example, is to have http://www.example.com/services/ .
With the code, when I type that URL into the browsers address bar, I have a web page without CSS.
Most likely the problem is relative versus absolute notation of the css file urls.
Consult the http servers error log to see what is actually referenced or check the delivered html source of the page that lacks the style definitions.
The page you specify in your comment to the question shows both, relative and absolute references of css files. However you have no rewriting rules for those...
That works as usual. Just ensure you set the right link to your CSS files so that the browser is able to request them without 404's from your server.
As you have probably edited the URLs within the .htaccess files after you've written the php script, you need to reflect these changes in your PHP script, too, by updting the links in there, too.
Monitor your server error log to find out which URIs are wrongly send.
Review those.
Then improve the output of your application by fixing that erroneous output.
Please see as well:
do-it-yourself universal header/footer.php
and similar questions that are about the root of your problem.

Two htaccess files

I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?

mod_rewrite redirect from any page to home page

I need to redirect if someone goes to any folder e.g. http://site.com/images/ or http://site.com/images to http://site.com.
Unless he goes to file e.g. http://site.com/images/index.php in this case it does not redirect
now i use
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
but i think its not perfect because e.g. if someone comes on http://www.site.com it does not work.
Keep in mind due to server configuration i need to put .htaccess in every folder.
I believe that what you are looking for is the -f flag:
RewriteCond %{REQUEST_FILENAME} !-f
See http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you are only looking at stopping someone from getting a list of the files you have in the folder, you may want to consider instead adding:
Options -Indexes
This will tell apache not to display a directory list if no index page is present in the given directory.
See http://httpd.apache.org/docs/2.2/mod/core.html#options
The last thing to note, if you are not familiar with how .htaccess works, apache scans the current folder and any of its parent folders - you should be okay with only adding a single .htaccess file in your web root for http://site.com. This has been covered in another question here.

Categories