Forum Moderators: coopster

Message Too Old, No Replies

Need help with mod_rewrite: changing dynamic links into static links

Newbie's first try with .htaccess files

         

PipSqueak

3:59 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



I couldn't get my site to show static links. I'm trying to change

[domain.com...]

to

[domain.com...]

and I used this in my .htaccess file:


Options +FollowSymLinks
RewriteEngine On
RewriteRule ^zenegra_sildenafil_citrate\.html$ meds.php?srch=21
RewriteRule ^generic_levitra\.html$ meds.php?srch=22

The .htaccess file is kept in the pills directory. I checked with a person who uses the same host as I do, and he says the mod_rewrite is enabled.

Do you know what's wrong with my code? Do I have to CHMOD the .htaccess file?

dreamcatcher

5:46 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi PipSqueak,

Is the mod-rewrite generator any help?

[webmaster-toolkit.com...]

dc

jd01

6:04 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you can't change the links, you don't have much chance if you are trying to be SE friendly -- It can be done (complicated), but your links will point to page.php?page=blahstuff, and the page will be at the/htmlversion.html. Does not sound like a very good idea to point every link on your site to a page that is permanently redirected.

If you really would like to do it, and cannot change your links, you will need to use %{THE_REQUEST} and redirect to the static pages, then use %{QUERY_STRING} to serve the information from the dynamic page to the static location.

I would suggest starting with the Apache Forum (in the library), and then the Apache Documentation.

Justin

ergophobe

7:29 pm on Sep 11, 2005 (gmt 0)

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



Yep, the apache forum is the place for this sort of thing. That said,


RewriteRule ^zenegra_sildenafil_citrate\.html$ meds.php?srch=21
RewriteRule ^generic_levitra\.html$ meds.php?srch=22

You're saying, if the url, after the host, starts with "zenegra..."

But it doesn't. It starts with "pills/zenegra..." and rewrites to /pills/meds.php... So you need to either

- drop the ^
- include the path from the very start.

PipSqueak

1:22 pm on Sep 12, 2005 (gmt 0)

10+ Year Member



No glimpse of hope ... :(

I tried this from rewriterule generator:


Options +FollowSymLinks
RewriteEngine on
RewriteRule zenegra(.*)\.htm$ /pills/meds.php?srch=$1

and this:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^meds/([A-Za-z0-9-]+)\.htm$ http://domain.com/pills/meds.php?srch=$1 [L]

just to see if there's any response to it. But none..

:(

PipSqueak

11:52 am on Sep 13, 2005 (gmt 0)

10+ Year Member



Perhaps someone can provide me with some simple code so that I can test whether I can use .htaccess at all?

jd01

5:28 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

# Options +FollowSymLinks
RewriteEngine on
RewriteRule ^fakepage\.html$ http://yoursite.com/ [R,L]

Then type yoursite.com/fakepage.html in your browser. Try as is... if you do not get a response try uncommenting the Options line.

Let us know what you get.

The generator will probably not help you get this one right -- here is an example: /zenegra_sildenafil_citrate\.html

** You will need the number that corresponds to the srch=NUMBER in the static URL somewhere, or you will need a separate rule for every page... (You may anyway, but that will depend on the structure of both friendly and un-friendly URLs.

/NUM/zenegra_sildenafil_citrate\.html

RewriteCond %{THE_REQUEST} \?scrh=([0-9]+)\ /HTTP
RewriteRule ^meds\.php$ http://yoursite.com/%1/zenegra_sildenafil_citrate\.html [R=301,L]

RewriteRule ^([0-9]+)/zenegra_sildenafil_citrate\.html$ /meds\.php?srch=$1 [L]

Justin

PipSqueak

2:28 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



the fakepage.html page redirected to my home page. I tried the other code you gave me as well, but it didn't work as well. Does this have anything to do with putting .htaccess in the "pills" folder?

jd01

6:51 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does this have anything to do with putting .htaccess in the "pills" folder?

It depends on the path to the file you are tyring to rewrite.

In the .htaccess the left side of the rule should be the full, server relative, path to the file -leading / and the right side of the rule should be the full, server relative, path to the 2nd location +leading / (For a redirect (seen in the browser) the right side path should be a canonical URL) EG

RewriteRule ^pathto/the/file\.html$ /pathto/theother/file.php [L]

So, the correction of my rules would be:

RewriteCond %{THE_REQUEST} \?scrh=([0-9]+)\ /HTTP
RewriteRule ^pills/meds\.php$ http://yoursite.com/%1/zenegra_sildenafil_citrate\.html [R=301,L]

RewriteRule ^pills/([0-9]+)/([^.]+)\.html$ /meds\.php?srch=$1 [L]

**Changed above to a regular expression -- will capture anyting that is not a .(dot)**

Justin

Added: Remember you will need to corresponding # in the static url if that is how you are going to reference the correct page in you php script.