Forum Moderators: phranque

Message Too Old, No Replies

rewrite to allow dot in it

rewrite to allow dot in it

         

drooh

11:16 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



RewriteRule ^([a-zA-Z0-9_-]+)$ index.phtml?page_name=$1

How would i modify this to allow for

website.com/1.php

right now when you hit that link or one like it you get a
404 not found

jdMorgan

12:29 am on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Allow dot, improve portability across servers, and increase speed by 30%:

RewriteRule ^([a-z0-9._\-]+)$ index.phtml?page_name=$1 [NC,L]

Jim

drooh

1:29 am on Jan 16, 2010 (gmt 0)

10+ Year Member



I just tried that and it does not seem to work...it doesnt allow the pages that are there to work, for instance

website.com/about_us/ does exist but with this htaccess it goes to my custom 404 page

jdMorgan

1:53 am on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Flush your browser cache and re-test. If that doesn't help, look elsewhere, as the problem is not in this rule.

Jim

drooh

3:34 am on Jan 16, 2010 (gmt 0)

10+ Year Member



Ok ive got one file .htaccess that contains this

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

jdMorgan

4:15 am on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, so it's rewriting URLs with periods in them now...
But it's also rewriting the previously-rewritten request for "index.phtml" to itself.
So exclude that path from being rewritten:

RewriteCond $1 !^index\.phtml$
RewriteRule ^([a-z0-9._\-]+)$ index.phtml?page_name=$1 [NC,L]

Note that your script will now have to properly handle requests for robots.txt, sitemap.xml, favicon.ico, and all image, css, media, and "document" requests, unless you also exclude those from this rule. I suspect you'll want to do that...

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

drooh

4:53 am on Jan 16, 2010 (gmt 0)

10+ Year Member



Jim , would you mind pointing me to some typical htaccess files that you would recommend? thanks for you help :)

jdMorgan

3:04 pm on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No such thing... Every single one I've ever seen (and that's a lot) have been different.

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

drooh

8:35 pm on Jan 16, 2010 (gmt 0)

10+ Year Member



thanks jd for your words. the problem is that your so restrictive that im not learning anything!

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.

jdMorgan

10:25 pm on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don' know about "fear" -- There's a whole library of threads here, and three different search functions available.

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

drooh

7:06 am on Jan 17, 2010 (gmt 0)

10+ Year Member



Jim if you have a moment would you look over this. This is my htaccess file ive been working on now for who knows how long. I am wondering if you see any red flags.

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

g1smd

8:42 am on Jan 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I see several problems.

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.