Forum Moderators: phranque

Message Too Old, No Replies

Converting 2 url problems

problem doing .Htaccess rewrites, Mod_Rewrite

         

aceofspade

5:21 am on Dec 24, 2010 (gmt 0)

10+ Year Member



I've 2 original url, say :

www.somesite.com/cgi-bin/profile.cgi?john
www.somesite.com/cgi-bin/keywords.cgi?john

I want to convert above url's into clean & short url like this:

www.somesite.com/john
www.somesite.com/keywords/john

I tried:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule (.*)$ cgi-bin/profile.cgi?$1
RewriteRule keywords/(.*)$ cgi-bin/keywords.cgi?$1

They don't work.

Kindly advise what did I do wrong?

g1smd

4:25 pm on Dec 24, 2010 (gmt 0)

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



Rules need the [L] flag on both.

The two rules should swap order, as the first rule matches all requests. The second rule never gets a chance to run.

In order to "change" the URLs you must change the links on your pages to mention the new URLs. It is links that define URLs.

The .* pattern matches all requests including those for images, stylesheets and scripts so you should change the pattern to
^([^/\.]+)$
or similar (so that it matches all requests without a file extension).

aceofspade

5:14 am on Dec 25, 2010 (gmt 0)

10+ Year Member



Hi,

Thanks for your reply.

As per your suggestion, I did the followings:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule keywords/^([^/\.]+)$ cgi-bin/keywords.cgi?q=$1
RewriteRule ^([^/\.]+)$ cgi-bin/webprofile.cgi?$1

However, they did not work because when I accessed the following url:

www.somesite.com/john
www.somesite.com/keywords/john

I could not get the page, but received a NOT FOUND Page with the following error message:

The requested URL /john was not found on this server.
The requested URL /keywords/john was not found on this server.

Kindly advise what did I do wrong?

aceofspade

5:38 am on Dec 25, 2010 (gmt 0)

10+ Year Member



Update to my previous post. I found a mistake I did:

RewriteRule keywords/^([^/\.]+)$ cgi-bin/keywords.cgi?q=$1

should be:
RewriteRule keywords/([^/\.]+)$ cgi-bin/keywords.cgi?q=$1

So the new .htaccess file is like the followings:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule keywords/([^/\.]+)$ cgi-bin/keywords.cgi?q=$1
RewriteRule ^([^/\.]+)$ cgi-bin/webprofile.cgi?$1

It works for:

www.somesite.com/keywords/john

But it does not work for:

www.somesite.com/john

How to fix the above problem?

g1smd

10:41 am on Dec 25, 2010 (gmt 0)

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



Make sure you add the [L] flag to both rules.

Flush browser cache before testing again.

aceofspade

4:14 pm on Dec 25, 2010 (gmt 0)

10+ Year Member



OK.. this is what I did:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !keywords\.cgi
RewriteRule keywords/([^/\.]+)$ cgi-bin/keywords.cgi?$1 [L]
RewriteCond %{REQUEST_URI} !profile\.cgi
RewriteRule ^([^/\.]+)$ cgi-bin/profile.cgi?$1 [L]

This time it works for:

www.somesite.com/john

But it does not work for:

www.somesite.com/keywords/john

I can only get one url going, but not both. Yak!

How to fix the problem?

jdMorgan

4:16 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rewriteconds are not needed, since a request for <anything>.cgi will not match the rule pattern, making those explicit exclusions unnecessary. Simplify the code to this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#
RewriteRule ^keywords/([^/.]+)$ /cgi-bin/keywords.cgi?$1 [L]
#
RewriteRule ^([^/.]+)$ /cgi-bin/profile.cgi?$1 [L]

I'm assuming that this code is going into a .htaccess file at example.com/.htaccess
If not, please specify the location of the file where you're putting this code.

Also, it may be that your server config is non-standard. If that is the case, then you may need to add
RewriteOptions Inherit

in your top-level .htaccess or config file.
Otherwise, subdirectories (such as '/keywords') will not inherit the rules of their parent directories. This option is usually set in the server config, but sometimes it isn't, and so has to be set manually.

Jim

aceofspade

1:19 pm on Jan 6, 2011 (gmt 0)

10+ Year Member



Hi,

Thanks for your assistance.

First, I'd like to confirm that the code is going into .htaccess file at www.somesite.com/.htaccess

(Although, when we try to access it:
www.somesite.com/.htaccess

it will say Forbidden
You don't have permission to access /.htaccess on this server.)

Second, I tried your code, it does not work without rewriteconds. Rewriteconds are needed. That is when the code is like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !profile\.cgi
RewriteRule ^([^/\.]+)$ cgi-bin/profile.cgi?$1 [L]

It will work for www.somesite.com/john

1) Without RewriteCond, i.e.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/\.]+)$ cgi-bin/profile.cgi?$1 [L]

It does not work because when I access: www.somesite.com/john

It will catch 'cgi-bin/profile.cgi' as the variable instead of 'john'.

2) But after I use the above code with rewriteconds, I CANNOT use the sub-directories in the site for anthing else:

a) Let's say I've some image files in a image folder like the followings:

www.somesite.com/image/pic1.jpg

It worked before I added the above code because I can access pics1.jpg file like this:

www.somesite.com/image/pic1.jpg

pics1.jpg file CANNOT be accessed after I added the above code.

b) It will NOT work for www.somesite.com/keywords/john even after I add another RewriteRule for a subdirectory 'keywords' like the followings:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule keywords/([^/\.]+)$ cgi-bin/keywords.cgi?$1 [L]
RewriteCond %{REQUEST_URI} !profile\.cgi
RewriteRule ^([^/\.]+)$ cgi-bin/profile.cgi?$1 [L]

Third, the only way it will work is like this:

www.somesite.com/profile/john ---> www.somesite.com/cgi-bin/profile.cgi?john
www.somesite.com/keywords/john ---> www.somesite.com/cgi-bin/keywords.cgi?john

The code for the above is like the followings:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule keywords/([^/\.]+)$ cgi-bin/keywords.cgi?$1 [L]
RewriteRule profile/([^/\.]+)$ cgi-bin/profile.cgi?$1 [L]

There is no way I can make my site works like the way I like:

www.somesite.com/john ---> www.somesite.com/cgi-bin/profile.cgi?john
www.somesite.com/keywords/john ---> www.somesite.com/cgi-bin/keywords.cgi?john

Unless you can pull a hat trick for me.