Jack Trowbridge
Jack Trowbridge

Reputation: 3251

htaccess mod rewrite confusion

I am trying to apply mod rewrites to my URL to make it nicer but I am getting caught up in all the confusion of this subject.

Please could you help me out with the following examples of what I want to do :

I want to attach any variable from the root of my site into the lyprofile.php page

# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{THE_REQUEST} /(.*)
RewriteRule /(.*) lyprofile.php?us=$1

I want a url such as profile/settings to go to lysettings.php

# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{THE_REQUEST} /profile/settings
RewriteRule /profile/settings lysettings.php

These two examples if working should help me to work out all my other URL's, but I can't get these to work.

Also do you need an absolute URL, as I'm working on my local machine and an absolute URL would just cause a lot of hassle. Just in case my absolute URL is http://localhost/Linku2.5/

Upvotes: 0

Views: 45

Answers (2)

Jon Lin
Jon Lin

Reputation: 143876

You generally want to go from the most specific rules to the least specific. Of the two things that you want to do, the first is the least specific, as (.*) can be anything. So we have to start with the much more specific and less arbitrary /profile/settings.

If these rules are in your htaccess file (in your case, in the Link2.5 directory), you don't want a leading slash, so simply:

# you only need to turn on once
RewriteEngine On

# Don't need absolute URLs, but you may need this line:
RewriteBase /Link2.5/

RewriteRule ^profile/settings lysettings.php [L]

Then, because your other rule is so general, you need to add some conditions so that you don't cause an infinite loop (the rewrite engine will continue to loop until the URI stops changing):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ lyprofile.php?us=$1 [L,QSA]

If you know your "us" variable can only be numbers or letters, you can make the line with the rule a bit more specific:

RewriteRule ^([A-Za-z0-9]+)$ lyprofile.php?us=$1 [L,QSA]

etc.

Upvotes: 1

jacouh
jacouh

Reputation: 8741

Can you check this in vhosts config?

# Turn mod_rewrite on
RewriteEngine On

RewriteRule lyprofile\.php - [L]

RewriteCond %{REQUEST_URI} /(.*)
RewriteRule /(.*) lyprofile.php?us=$1 [L]

RewriteCond %{REQUEST_URI} /profile/settings
RewriteRule /profile/settings lysettings.php [L]

Upvotes: 0

Related Questions