Apache rewrite for a single file

Hi,

this is a very simple question but i have tried the Sitepoint article of 13 reals world examples to rewrite URL’s. I want to redirect only two files

RewriteCond %{REQUEST_URI} !^signup.php
RewriteRule ^/?([a-zA-Z0-9]+)$ signup.php [L]

RewriteCond %{REQUEST_URI} !^learnmore.php
RewriteRule ^/?([a-zA-Z0-9]+)$ learnmore.php [L]

Now the problem is that signup and learn more both redirects to signup

i searched but it didn’t work out pretty well and i am a designer :slight_smile:

somebody help

What do you want to redirect to what?
The way it is now it seems you want redirect every request to signup.php …

yes that is exactly what is happening
i was thinking that RewriteCond works only once
so how would we redirect

signup.php->signup
and
learnmore.php->learnmore

is there any tutorial where i can understand more, 've been repeatedly reading :stuck_out_tongue: and it just doesn’t click to me :slight_smile:

Just to be safe, could you confirm your urls are:

(www)yoursite.com/signup.php and
(www)youtsite.com/learnmore.php

and you want to get rid of the .php at the end (that people only see signup and learnmore in their browsers and only have to type those in to get to the correct page)?

so that it is (www)yoursite.com/signup and
(www)yoursite.com/learnmore
?

If true, you will only do a rewriteRule, and won’t use rewriteCond at all. Pls confirm?

is there any tutorial where i can understand more, 've been repeatedly reading :stuck_out_tongue: and it just doesn’t click to me

http://datakoncepts.com/seo give that one a try. It’s written pretty simple or you can look around for the part that looks like it does what you’re after.

yes.
that is what i want. i dont want the php extension.
i could do this successfully from this article http://articles.sitepoint.com/article/apache-mod_rewrite-examples

but i want to get rid of the php extension only for learnmore.php and signup.php

all the other files i want to have the extensions for php.

Ah!

Then what you do is make the match more specific. The bad regex examples have
RewriteRule (.*)$ $1.php [R=301]

and the good ones might have
RewriteRule ^([^/][-a-zA-Z._]+)/?$ $1.php [R=301]
or something. Remember the first section is what you’re matching and because it might be many things, people use the regular expressions.

But in your case you really don’t want to match “any letters, dots, underscores”… you want to match
“learnmore” OR “signup” exactly.

From this point on, this is a guess… I’m not 100% certain if the pipe will be “or” this way…

RewriteRule ^(learnmore | signup)$ $1.php [R=301]

This means the real file is still learnmore.php or signup.php. You’re letting people either type in yoursite.com/learnmore and they’re getting silently redirected to yoursite.com/learnmore.php
etc

None of your other pages match those exact words so they will not go through the rewrite. If I did it right (Scallio would know better lawlz), then whatever’s matched, whether it’s learnmore or signup, gets added where the $1 is.

thanks Stomme poes

let me try this :slight_smile: thanks

hi
i used this and it seems working

Options +FollowSymLinks

RewriteEngine on
RewriteRule ^/?([test1|test2]+)$ $1.php [L]

i don’t know what that + is supposed to do but it works. :stuck_out_tongue:

Altough it works, it is semantically incorrect, since are used for character classes.

What you want is


Options +FollowSymLinks

RewriteEngine on
RewriteRule ^/?(test1|test2)$  $1.php [L]

Agreed… [stuff in here] is generally one character to match, and the reason ([test1|test2]+) worked is the + which means “any number but at least one”, which kind of gets around that (in a way that you don’t want).

ravi,

XTX and Sp have done a good job leading you in the correct direction. However, please let me stress a few points:

  1. If you want a link which is not to a file, YOU must create that link, not mod_rewrite! signup is not a file nor is it a directory so it should draw a 404 error. mod_rewrite’s job is to redirect signup to signup.php (hidden) to give you the extensionless look of your URI (the same was that seo in http://datakoncepts.com/seo is an extensionless URI - it’s the seo.php script’s output which is presented to the visitor).

  2. The above link is to the tutorial I created to preserve my wrists (repetitive typing - answering the same posts over and over and …).

  3. A good specification is where you must always start: signup => signup.php and learnmore => learnmore.php should be sufficient. Those will tell you that NOT signup.php being redirected to signup.php is NOT what you want. It should be

RewriteRule ^/?signup$ signup.php [L]

Same with learnmore.

As suggested, these TWO RewriteRules can be combined very nicely with:

RewriteRule ^/?(signup|learnmore)$ $1.php [L]

Both XTX and Sp correctly removed the 's from your implementation as they are metacharacters used to create a character list whereas the ()'s create an atom. The pipe in the atom tells Apache to match the string before the pipe OR the string after the pipe (these can be chained if you have more extensionless links you want to use) and that becomes Apache’s $1 variable for the redirection.

NOTE: The ^/? is used because you did not let us know whether your server is Apache 1 (which requires ^/) or Apache 2 (which will NOT match the / so it requires only ^) - the ^/? can be used when you don’t know which version of Apache you’re using. :nono:

Regards,

DK

thanks a lot DK, XTX and Sp.

this is working great for me. now i understood :slight_smile: