Jump to content

Can't get mod_rewrite to work Friendly URL to Access Web Page


TecTao

Recommended Posts

On a membership site, a user has a code consisting of two capital letters and and series of numbers, such as FL830.

 

We are giving them one page web pages, website.php, that will query the DB and display user information.

 

What I want if for a viewer to only have to type in www.examplesite.com/FL830

 

They would ending seeing the page as if it were a GET request www.examplesite.com/webpage.php?rep_id=FL830

 

I have written a mod_rewrite in the .htaccess file in the root of the site.  The following code is

 

The rule that I'm trying to write is from the friendly URL to the GET request URL. I don't know if this is possible.

 

Here's the code I've writtne

 

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).com/([A-Z]+)-([0-9]+)$ (.*).com/webpage.php?rep_id=$1-$2 [L]

 

The (.*).com/([A-Z]+)-([0-9]+) is the website URL/rep_id code.

 

The (.*).com/webpage.php?rep_id=$1-$2 is translating it back to the GET request with two masks, the Alpha part and the Numeric part

 

This is not working. 

 

If I type in the URL www.marketingteammates.com/webpage.php?rep_id=FL830 the page comes up with the users information.  If I type in www.marketingteammates.com/FL830 it errors with a 404 page not found.

Link to comment
Share on other sites

Your rewrite rule is looking for one or more uppercase letters followed by a dash followed by one or more digits. Your description of the problem and your example do not indicate there should be a dash there. Your rewrite rule is also supplying a dash in the GET variable; again, not mentioned in the problem statement. From the description of the problem and the example, I would use:

 

RewriteRule ^([A-Z]{2,2}[0-9]+)$ webpage.php?rep_id=$1 [L]

 

Also, I just noticed, you are capturing the domain name so IT would be $1, the letters would be $2 and the digits would be $3.

 

Oh yeah, and you don't get the domain name for the rewrite rule, you only get the page (and directories)

Link to comment
Share on other sites

Thanks, I picked up on part of that too, the new rule I've uploaded is

 

RewriteRule ^([A-Z])([A-Z])([0-9]+)$ /webpage.php?rep_id=$1$2$3 [R,NC,L]

 

I believe it's now looking for one capital letter next to one capital letter next to any series of numbers.  I'm assuming that the variable that is passed would now be rep_id=FL830

 

Problem is I'm still getting a 404 page does not exist.

Link to comment
Share on other sites

Thanks, your observation caught it and it works. 

 

So, now I have a question whether it can have a new rule that would reverse itself.

 

When I put in www.examplesite.com/FL830 it refreshes to the page and the address bar shows www.examplesite.com/webpage.php?rep_id=FL830

 

I would like it to just show the www.examplesite.com/FL830 in the address bar

 

I tried writing a new rule but that did the opposite of the first rule and it didn't work.  Any thoughts on part of the rewriteRule I missed?

 

Current rule is

 

RewriteRule ^([A-Za-z])([A-Za-z])([0-9]+)$ /webpage.php?rep_id=$1$2$3 [R,NC,L]

 

Thanks again for the earlier help.

Link to comment
Share on other sites

That has to do with the flags you have on the rewrite rule. The "R" tells Apache to redirect to the url that you have built. Take the "R" out and it should work as you expect.

 

Also, there is no reason to capture those values separately, you can use a single capture group

RewriteRule ^([A-Za-z][A-Za-z][0-9]+)$ /webpage.php?rep_id=$1 [NC,L]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.