Forum Moderators: phranque

Message Too Old, No Replies

How to get only certain pages to re-write?

         

emotn

10:54 am on Jan 11, 2012 (gmt 0)

10+ Year Member



I am new to .htaccess and have just got one rewrite working thanks to the people here.

Now I need to know how I can get that one rewrite to only rewrite when the topic.php is called and no other page

Here is my .htaccess file

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^([A-Z0-9_-]+)?$ topic.php?Topic=$1 [NC,L]


I just want the rewrite to work on any topic.php file and not on any other file.

The main one I am having the trouble with is the index.php when it is called as www.example.com/ then the topic.php comes up instead and of course has no data. if I call www.example.com/index.php it works fine.

I have tried

RewriteRule ^/index.php  -   [L,I]


as I thought that was a "don't rewrite" command but I get an internal server error

Again, any help and guidance will be much appreciated :)

lucy24

11:17 am on Jan 11, 2012 (gmt 0)

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



RewriteRule ^topic\.php$ et cetera

Take a quick look at the nearby posts in this Forum. I think someone else just had the identical issue, because your question sounds awfully familiar :)

Is there such a thing as an [I] flag? Never heard of it, and can't find it. Might explain the server's sulks.

emotn

2:06 pm on Jan 11, 2012 (gmt 0)

10+ Year Member



LOL - yep - the
[I]
was making the server sulk :)

I searched and read and found the following

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php[?]? [NC]
RewriteRule ^index\.php$ http://www.example.com/ [NC,R=301,L]


but when I go to http://www.example.com/ it is still pulling up the topic.php file - I have a

$Pagename = basename ( $_SERVER['PHP_SELF'] ) ;
echo $Pagename;


At the top of the inc/top.php file that is included in all files so I can see what is happening.

If I put in http://www.example.com/index.php I get the right file and the right content.

So then I tried the following

RewriteCond %{THE_REQUEST} ^index\.php [NC]
RewriteRule ^index\.php http://www.example.com/ [NC,R=301,L]


as there is nothing before or after the index.php but still get the topic page .... :(

*deep sigh*

Any other tips?

Thank you :)

phranque

2:56 pm on Jan 11, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



%{THE_REQUEST} is going to begin with the HTTP request method such as GET so if you front-anchor that regexp with ^index it will fail on that condition.

emotn

3:34 pm on Jan 11, 2012 (gmt 0)

10+ Year Member



Thank you for explaining that - I will keep away from that now :)

I now have tried:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


# ### Testing to get the index.php to show when using the domain root only and not the example.com/index.php

#RewriteRule ^index\.php - [NC,R=301,L]

#RewriteRule ^index\.php$ http://www.example.com/ [NC,R=301,L]

#RewriteRule ^index\.php http://www.example.com/ [NC,R=301,L]



RewriteRule ^([A-Z0-9_-]+)?$ topic.php?Topic=$1 [NC,L]


None of the 3 # out have done it - there are also alot of other combos that I have tried.

I guess that the rewrite rule that works is saying "ANYTHING goes to topic.php" ?

which is why I have been putting the other test index.php rules before it but to no avail.. :(

I would have thought this was something easy as I see it all the time - sites with pretty links and their root is example.com without anything else after it.

Any other suggestions? Anything at all is really appreciated :)

thank you for your time.

lucy24

11:46 pm on Jan 11, 2012 (gmt 0)

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



Ah ha. You need to go back and search for the difference between rewrite and redirect. Both can be done using mod_rewrite; the difference is in wording:

RewriteRule (topic\.php) index.php?$1 [L]

RewriteRule (topic\.php) http://www.example.com/index.php?$1 [R=301,L]

The first version is a rewrite. User asks for topic.php, address bar continues to say topic.php, but user gets served content from index.php using their requested topic as a query string.

The second version is a redirect. User asks for topic.php, they get sent outside, address bar changes and they start all over again exactly as if they had asked for index.php plus query string.

This second version also uses redundancy. (In linguistics it's called "double markedness", which is a better term.) The protocol-plus-domain part and the R flag would each, by themselves, make it into a redirect. But without the 301 it defaults to 302; without explicit domain it defaults to whatever domain the user originally requested, with or without www., possibly even with appended port.


In case you wondered: Redirects will show up in your logs as 301 followed by a whole new request. Rewrites will simply say 200 followed by the filesize of the page the user actually got (which may be different from the size of the page they asked for, if both really exist). Robots can choose not to follow up on a redirect; humans have no choice. Neither humans nor robots have any control over rewrites.

g1smd

12:00 am on Jan 12, 2012 (gmt 0)

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



^([A-Z0-9_-]+)?$

The question mark says "stuff in parentheses is optional".

Taking that away gives ^$ as the pattern.

This pattern matches a request for example.com/ (as the hostname and leading slash is stripped before being passed to the pattern matching routine) so the topic.php file content is served.


You need to get all of the redirects and rewrites 100% correct. When a user requests the wrong version of a URL the redirects make sure the user makes a new request for the correct URL. The rewrites kick in when the user makes the right request and the rewrite ensures the right content is served.

emotn

1:17 am on Jan 12, 2012 (gmt 0)

10+ Year Member



I had just been playing with DirectoryIndex - which I couldn't get to work when I checked back here and saw your 2 replies and I really appreciate all of your time :D

@lucy24 - thank you for those explanations - now I know what to keep away from which is good! And also understanding the differences will help :)

@g1smd - perfect! getting rid of that ? solved it and now it works as I wanted it to :D