Forum Moderators: phranque
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-z0-9._\-]+)$ index.phtml?page_name=$1 [NC,L]
The ive got one php file index.phtml with the following code
<?
if(isset($_GET['page_name'])){
$page_name = $_GET['page_name'];
echo $page_name;
}
?>
And in the url whatever I type
website.com/something
I simple get this
index.phtml
printed out
RewriteCond $1 !^index\.phtml$
RewriteRule ^([a-z0-9._\-]+)$ index.phtml?page_name=$1 [NC,L]
Also, if this is all you've got in your .htaccess file, you may want to consider adding the 'standard' domain and index-page canonicalization redirects that we discuss here on a daily basis, so as to preclude duplicate-content problems before you get them. A few basic access-control rules may be in order as well -- For example, make sure that your .htaccess file itself is not accessible via HTTP...
Jim
There's no "easy way" or "one-size-fits-all" code. Study the documentation to gain understanding, write and test your own code to gain experience, or hire all of this out. Apache .htaccess code, like that in httpd.conf, conf.d, etc., is server configuration code and you shouldn't expect it to be easy, or to be a cut-and-paste proposition -- any more than the HTML pages of your Web site could be cut-and-pasted.
There are tons of threads in this forum and in our Library. You can take them for examples for the purposes of learning, but if you don't actually study them, then when you find a code snippet that 'looks just right' for what you want, how will you know if it is in fact "just right?"
The Charter for this forum emphasizes education and discussion -- and rejects the use of this forum as a pure "help desk" or "free coding service." And there's a very good reason for that: Every site is different, and very likely requires a different configuration set-up. When looking at previous threads without full understanding, it is quite possible to find an absolutely-perfect solution to the wrong problem; Installing that code may sink your site in the search results and put you out of business, even though it is technically perfect and purely-beneficial for the site that was described and discussed in that thread...
Jim
most people learn best with side by side help and working through a particular problem.
you write phrases that invoke fear but you do not supply a direction or instructions for solving it.
oh, and as for reading the documentation, i have, many many many hours! none that i have found have featured this example im stating.
We are (and I am) happy to give a few pointers, and to help those who are demonstrably helping themselves -- It's fairly easy to tell by the nature of the questions asked. But as for side-by-side help, I can't afford that investment of time. Like all the 'staff' here, I'm just a member who volunteered to help out, and I've got a 'real' job to do to feed my family. I'm pretty sure that most contributors here are in the same boat.
Try the Library or search this site for "redirect www non-www HTTP_HOST" and "Redirect index to / THE_REQUEST" (or try "root", or "slash" instead of the literal "/"). Those searches are made more-specific than normal by the addition of the "code" variables at the end, which will save you some time sorting through the results.
Searches for "domain canonicalization", "URL canonicalization", "index page canonicalization", and "prevent duplicate content" may also prove useful.
Take the threads that seem most-like what you want to do, and then take the code apart --one character at a time if necessary-- using the documentation cited in our Apache Forum Charter and not proceeding any further until everything is fully understood. Look up the directive starting each line, look up each regular-expressions token, and find their meaning. Once the details of what the code is doing at the line-by-line level are understood, take three steps back for an overview, and ask, "How will this affect URLs being requested from my server?"
I don't know that that is the best way, but it's how I figured it out some years ago... That plus reading the mod_rewrite documentation dozens of times, each time with a bit more understanding.
HTH,
Jim
Options +FollowSymlinks
RewriteEngine on
#actual forlders, redirect with slash and 404
RewriteRule ^inc$ /inc/
RewriteRule ^([a-zA-Z0-9_-]+)$ ?page_name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ ?page_name=$1
RewriteRule ^([0-9a-zA-z]+)/([0-9a-zA-z]+)$ ?page_name=$1&sub1=$2
RewriteRule ^([0-9a-zA-z]+)/([0-9a-zA-z]+)/$ ?page_name=$1&sub1=$2
RewriteRule ^([0-9a-zA-z]+)/([0-9a-zA-z]+)/([0-9a-zA-z]+)$ ?page_name=$1&sub1=$2&sub2=$3
RewriteRule ^([0-9a-zA-z]+)/([0-9a-zA-z]+)/([0-9a-zA-z]+)/$ ?page_name=$1&sub1=$2&sub2=$3
#custom error for files that do not exist
ErrorDocument 404 /?page_name=404
#custom forbidden page such as .htaccess
ErrorDocument 403 /?page_name=404
#actual files on server, redirect with 404
RewriteRule ^inc/[0-9a-zA-z]+.php$ /?page_name=404
Every rule needs the [L] flag added.
Using a script to generate the 404 error will cause problems if it is the script that has failed.
[0-9a-zA-z] can be replaced with [0-9a-z] used with the [NC] flag to speed it up. Note too, your typo of A-z instead of A-Z.
The rewrite substitution path should start with a / so I would have /index.php?page_name=$1 instead.
For each of your rules that is doubled up, you have created a Duplicate Content problem. That is, more than one URL will return the same content. To fix this, URL requests 'with trailing slash' should be redirected to 'URL without trailing slash' and the rewrite should only work for URL requests 'without trailing slash'. The redirect should force correct domain at the same time and must be a 301 redirect. There are several threads in the last week discussing this exact problem.
#actual files on server, redirect with 404
Terminology problem. The 404 needs to be served for the originally requested URL. There should be no 'redirect' here.