I have a site that has a news section in a subfolder which I have been adding to manually.
www.example.com/subfolder/news-and-views/name-of-article.php
I recently made a rather simplistic CRUD feature to make adding these articles easier.
I have:
www.example.com/subfolder/news-and-views/index.php - This displays all articles
www.example.com/subfolder/news-and-views/article.php - This takes an Id parameter from a $_GET request
The url I end up with when I view an article is:
www.example.com/subfolder/news-and-views/article.php?Id=name-of-article
I did some reading and used http://htaccess.mwl.be/ to test a RewriteRule
The rule is: RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
However when I go to news-and-vies/ (the index page) my CSS is stripped out and nothing is displayed as my paths are changed.
Also it seems to go to article.phpid?= without passing anything, rather than just using the actual index page.
To set paths I use a variable called $path2root which is the number of directories to the root.
E.g $path2root = "../../";
So: I'd have include $path2root . "includes/head.php"
Is my RewriteRule interferring with this variable?
My .htaccess file
RewriteEngine On
RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
My file structure
/ .htaccess at this level (root)
--/ subfolder
----/ news-and-views
------/ index of news and views
RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
As mentioned in my comment, you appear to have omitted the /subfolder, so I'm not sure how this rewrites successfully at all? You also need to be careful of rewriting existing files (eg. index.php and article.php and CSS?) and creating a rewrite loop.
Try something like the following instead:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(subfolder/news-and-views)/(.+) /$1/article.php?Id=$2.php [L]
Related
I'm struggling pretty hard with .htaccess. Even though I looked up quite a few solutions here, I couldn't get to setup the following:
I have a file called index.php. Inside the index.php I have a link like
Post
Clicking the link, should lead to a file in the same directory called post.php. Inside the post.php I'm grabbing the id through $GET['id'].
But I still like to display post/12345678901 as the URL.
I already tried editing the .htaccess but clicking the links leads to a 404.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^post/([^/]+) /post.php?id=$1
You have a RewriteRule but you probably need a ReWriteCondition to go with it else this rewrite will be applied to every call to a page on the apache.
Options +FollowSymlinks
RewriteEngine on
# Not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# Edited to show root location of files.
# also added NoCaseSensitivity flag.
RewriteRule ^post/([0-9]+)/? /post.php?id=$1 [NC]
Also remove the first / in the second part of the RewriteRule because that will be domain.com/post.php which might not be your actual file location.
Your HTML appears to be showing relative filepathing which is not a good idea and might come back to bite you in the future, As a recommendation every URL on the same domain on your HTML should start with a / so;
Post
Which can then be manipulated by the .htaccess to go where you need, even in another folder (not the base).
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.
The site I'm building at the moment is made of two main parts: The side which the general public can access, and the admin side which only authorised people can access.
It's built with basic templating such that the different sections are accessed as follow (Using RewriteRules).
Public:
http://localhost/about should be rewritten to http://localhost/index.php?page=about
Admin:
http://localhost/admin/manage-users should be rewritten to http://localhost/admin/index.php?page=manage-users
All URLs only ever have one argument. That is, public will always be localhost/PAGE and admin will always be localhost/admin/PAGE.
At the moment, I have the following .htaccess file:
RewriteEngine on
RewriteRule ^admin/([^/.]+)/?$ /admin/index.php?page=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,NC]
This seems to work properly when you construct the URL correctly. For example, if I navigate to localhost/about or localhost/admin/manage-users both pages load correctly. But if I go to localhost/about/blah or localhost/admin/manage-users/blah, the pages load, however the CSS is non-existant. Looking at the developer tools in Chrome, it appears that this is because it's trying to load the CSS file from the directories localhost/about/css/ and localhost/admin/css/ respectively, due to the style sheet being linked to the page with a relative path. (In reality, localhost/css/ is the directory it is actually located in.)
So even though the RedirectRule ignores any extra arguments in the URL, it is trying to load relative paths with respect to the last "directory" provided in the URL.
Is there any way to completely ignore any extra ../.. arguments? Or, even better, trigger a 404 when too many arguments are provided?
UPDATE: I have just discovered that the problem is actually a lot more complex than I previously thought. As my pages only had dummy data to test out the templating files, I didn't notice it until now.
It appears than when you navigate to localhost/admin or localhost/admin/manage-users it is loading from the http://localhost/admin/index.php file, but when you navigate to localhost/admin/manage-users/blah is reverts back to loading the http://localhost/index.php file. This makes me think that there is something I need to change in the RewriteRule, though I have no idea what.
It is better in long term to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
But in order to avoid making changes to your website in-mass you can use these rules to fix your css/js/images links:
RewriteEngine on
# fix CSS/js/images links
RewriteRule (?:^|/)((?:css|js|images)/.+)$ /$1 [L,R=301]
RewriteRule ^admin/([^.]+)/?$ /admin/index.php?page=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
Don't forget to replace first rule with your actual css/js/images directories.
You need to either make all of your links absolute URLs (e.g. href="/css/style.css") or add a relative URL base to the header of your page:
<base href="/" />
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.
To start, I am a beginner with PHP and .htaccess. Here is my dilemma...
I have built dynamic pages and used htaccess to rewrite the urls. There are 3 types of pages... Examples:
State: example.com/massachusetts-colleges.html
City: example.com/massachusetts-colleges/boston-ma-colleges.html
College: example.com/massachusetts-colleges/boston-ma-colleges/harvard.html
The problem is that pages are being requested (from old linking structure probably) that shouldn't exist such as:
example.com/boston-ma-colleges.html
The state urls are stored in a locations table in the database (stateSlug = massachusetts-colleges). The city urls are also stored in the locations table in the database and the corresponding state slug is also stored with that city (citySlug = boston-ma-colleges and stateSlug = massachusetts-colleges). The Colleges are stored in a different table and use ID's to correspond with the cities.
How can I use .htaccess to prevent any "OTHER" urls from being accessible (page displays template and no data), and show a 404 page (or redirect to home page)?
This is what my .htaccess file looks like now:
RewriteEngine on
RewriteRule ^([^/\.]+)\.html?$ php/statePage.php?stateSlug=$1 [L]
RewriteRule ^([^/\.]+)-colleges/([^/\.]+)\.html?$ php/cityPage.php?citySlug=$2&stateSlug=$1 [L]
RewriteRule ^([^/\.]+)-colleges/([^/\.]+)/([^/\.]+)\.html?$ php/collegePage.php?collegeSlug=$3&citySlug=$2&stateSlug=$1 [L]
Again, I am somewhat new to the htaccess and php languages. I would appreciate any help in this matter.
Thank you!
Assuming all of your content is matched by one of the above URLs, you can simply forbid access to everything else by adding another rule to the end:
RewriteRule ^(.*)$ - [F]
To avoid messing with requests your images and CSS, you should add:
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif|js|css)$ [NC]
RewriteRule ^(.*)$ - [F]
If you have directories which should be made inaccessible, place a .htaccess in them with only the following:
Order deny,allow
deny from all
Try the following, but replace index.php with your 404 page:
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$l [L]
This should redirect all file and directory requests that were not found to index.php or your choice of script file, a 404 page for example. Just remember to set the headers to a 404 response code.
I got this from the CodeIgniter wiki page at http://codeigniter.com/wiki/mod_rewrite/
Their wiki page was very helpful when I tried to solve a similar problem.
rewrite all your public files to fake directory /allow/.., display 404 for any request which is not in /allow/.. directory. Finally rewrite files from /allow/.. to real directory.
It's great workaround IMO :) Better than using deny all in every dir...