Forum Moderators: phranque

Message Too Old, No Replies

apache mod rewrite doesn't work

apache, modrewrite

         

bstras32

7:48 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



Here is a sample url: [localhost...]

httpd:
LoadModule rewrite_module modules/mod_rewrite.so

.htaccess: (located in WebFrame directory)
Options +FollowSymLinks
RewriteEngine on

RewriteRule /page/(.*)\.html ?page=$1

I can't seem to get this to work, I've even tried about a dozen generators out there and nothing is working, I know the .htaccess file is working because I put garbage in there and it blows up, please help.

g1smd

8:04 pm on Mar 15, 2010 (gmt 0)

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



This will accept a URL request like example.com/WebFrame/page/<something> but only if you remove the leading slash from the pattern. :)

RewriteRule cannot see that leading slash.

Perhaps you ought to add the filename of the script (whatever it is called on the server, that is) before the question mark just to be sure, too.

Add the [L] flag to the rule.

Once you have it working, separately add a redirect to the new URL for whenever there is an external request for a URL like
example.com/index.php?page=<something>
or
example.com/?page=<something>
.

bstras32

8:19 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



I tried this:

Options +FollowSymLinks
RewriteEngine on

RewriteRule page/(.*)\.html index.php?page=$1[L]

but no luck, the darn thing just doesn't seem to want to change.

g1smd

8:23 pm on Mar 15, 2010 (gmt 0)

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



Change? Change what?

A RewriteRule doesn't 'make' a URL. You need to link to
example.com/page/<whatever>.html
on the pages of your site.

The RewriteRule will then kick in and fetch the content from
/index.php?page=<something>
inside the server.

The (.*) pattern can be improved by changing it to ([^.]+) too.

bstras32

8:38 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



Thanks! I understand it now! I got it working, all I had to do was just change the link. Can't believe I spent so much time on this for something so simple, thanks again! I had this posted on Experts Exchange all weekend without a single reply, you rock!

g1smd

8:48 pm on Mar 15, 2010 (gmt 0)

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



Don't forget to add the additional redirect that I mentioned above. This will be two separate lines of code listed before your rewrite.

You will need a RewriteCond looking at THE_REQUEST as well as the RewriteRule that performs the 301 redirect. It will need to work for requests like
example.com/index.php?page=<something>
as well as
example.com/?page=<something>
. The target will this time be a URL (with domain name) rather than a server file. You'll need the [R=301] flag in addition to the [L] flag now.

bstras32

9:20 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



This is probably a stupid question but why would I need the additional lines of code if the rewrite rule is working? I would only need this if I had a site indexed already correct? This is my first attempt at any of this so I apologize if I am asking something obvious.

g1smd

9:32 pm on Mar 15, 2010 (gmt 0)

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



You have a file at
index.php
and passing a parameter of
?page=<something>
to it should generate content.

That file can be accessed using these URLs:
1.
example.com/?page=<something>

2.
example.com/index.php?page=<something>

3.
example.com/WebFrame/page/<something>

whether or not YOU link to them.

Your rewrite connects requests for URLs in format '3' to the script. The additional lines of code will accept URLs in format '1' or '2' and redirect the user to make a new request for the URL in format '3'. Your site needs to 301 redirect non-canonical URL requests for content to the correct URL version to prevent Duplicate Content issues.

For any value of <something> for which no content exists, your script also needs to return a 404 HTTP Header and HTML code explaining that an error has occurred.

bstras32

8:59 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



Thanks for all your help I have everything working now except setting up the script so I don't have to repeat it for every page. Here is what I have:

RewriteCond %{THE_REQUEST} page=(.....)
RewriteRule (.*) http://localhost/WebFrame/%1\.html? [R=301,L]

Now obviously this works if every page was 5 letters long but it won't be and I tried a bunch of different regex with no success any suggestions?

[edited by: jdMorgan at 9:12 pm (utc) on Mar 16, 2010]
[edit reason] De-linked localhost URL in RewriteRule. [/edit]

jdMorgan

9:18 pm on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not clear if this is what you want, but this takes *any requested URL* that has a "page=" query string attached, and redirects it to http://localhost/WebFrame/<query-string-page-parameter>\.html

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?]*)\?([^&]*&)*page=([^&]+)
RewriteRule ^ http://localhost/WebFrame/%1\.html? [R=301,L]

Jim

bstras32

11:02 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



I tried the code and it seems that it is adding an extra PHP on the end of the index.php page. Here is the warning:

Warning: include(WebFrame/index.php.php)

bstras32

11:14 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



Also it is adding 2 WebFrame directories. Here is the url:

[localhost...]

bstras32

2:43 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Ok so now I have everything working except the [OR]. Both conditions work separately but not together. Where is my [OR] going wrong?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /WebFrame/index\.php\?page=([a-z0-9]+) [NC][OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /WebFrame/\?page=([a-z0-9]+) [NC]
RewriteRule (.*) [localhost...] [R=301,L]

bstras32

2:47 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Never mind, I found it:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /WebFrame/index\.php\?page=([a-z0-9]+) [NC,OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /WebFrame/\?page=([a-z0-9]+) [NC]
RewriteRule (.*) http://localhost/WebFrame/%1.html? [R=301,L]

[edited by: jdMorgan at 7:08 pm (utc) on Mar 17, 2010]
[edit reason] de-linked localhost URL for clarity. [/edit]

g1smd

8:00 pm on Mar 17, 2010 (gmt 0)

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



The OR can be implemented a different way, using just one line:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /WebFrame/[b]([/b]index\.php[b])?[/b]\?page=([a-z0-9]+) [NC]

g1smd

8:35 pm on Mar 17, 2010 (gmt 0)

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



Parallel conversation: [webmasterworld.com...]