Allowing a . within the Rewrite Rule

Using mod_rewrite to handle various content issues

Allowing a . within the Rewrite Rule

Postby anawaz » Wed Jul 15, 2009 12:25 pm

Okay, so here is what I've got:

http://internal.vafta.com

When we enter a URL (asifism.com) and press enter, we get

http://internal.vafta.com/index.php?vwe ... sifism.com

Okay, all good. Here is what my .htaccess file has:

RewriteEngine On

RewriteRule ^(.*)-(.*)$ index.php?vwebsite=$1.$2 [L]

So, this basically means that I can type: http://internal.vafta.com/asifism-com in my address bar and that passes asifism.com to my script, thereby executing it propertly.

How can I write the Rewrite Rule so I don't have to use asifism-com in the URL and I can use asifism.com. What do I do to make it accept the . instead of the -

Help will be appreciated.
anawaz
 
Posts: 7
Joined: Wed Jul 15, 2009 1:49 am
Location: London

Postby richardk » Wed Jul 15, 2009 12:57 pm

You can escape special characters with a "\", eg "\.".

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+\.[^/]+)$ /index.php?vwebsite=$1 [L]


Regular Expressions: Characters
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Not Quite working

Postby anawaz » Wed Jul 15, 2009 2:06 pm

Thanks Richard. I've tried using the \ before, but it hasn't quite done the trick. Using your code I get the 500 internal server error (which probably occured because I don't have a 404 setup). Now, if I remove the / before the index.php?vwebsite=$1, then I end up passing through index.php as the variable instead.

So, same question as the first time around, plus an additional second question, what if my index.php sat in a subdirectory. Then what would I use to pass the subdirectory?

For instance, if the URL was http://internal.vafta.com/siteworks/ind ... atever.com

Thanks!
anawaz
 
Posts: 7
Joined: Wed Jul 15, 2009 1:49 am
Location: London

Postby richardk » Thu Jul 16, 2009 1:09 pm

Now, if I remove the / before the index.php?vwebsite=$1, then I end up passing through index.php as the variable instead.

Because it matches the internal request to /index.php?vwebsite=example.com
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+\.[^/]+)$ ./index.php?vwebsite=$1 [L]


So, same question as the first time around, plus an additional second question, what if my index.php sat in a subdirectory. Then what would I use to pass the subdirectory?

You would use a relative or correct full path to index.php.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

worked like a charm!

Postby anawaz » Fri Jul 17, 2009 6:23 am

Thanks Richard. This worked like a charm!

Here is another one.

The current .htaccess file has the following to allow for SEO friendly URLS:

Rewriterule ^(images|CMS|cmsm|downloads|documents|intelligence|js|marketing|newsletters|scripts|__includes|clients|specific_scripts) - [L]
RewriteRule ^(.*)_(.*)$ /index.php?tag=$1&page=$2/ [L]

So, basically, this allows me to use URLS like http://www.vafta.com/our-clients_33, which is good because that's far more SEO friendly than vafta.com/?=33.

Now, I'm trying to do the same thing with the files sitting in the cms directory (http://www.vafta.com/cms).

Here's what I added:

RewriteRule ^cms/(.*)_(.*)$ /cms/index.php?tag=$1&page=$2/ [L]

But this doesn't seem to work for some reason. If I now try http://www.vafta.com/cms/free-demo_16 or something to that effect it doesn't work. Any idea why?

Thanks!
anawaz
 
Posts: 7
Joined: Wed Jul 15, 2009 1:49 am
Location: London

Postby richardk » Fri Jul 17, 2009 10:32 am

Code: Select all
Rewriterule ^(images|CMS|cmsm|downloads|documents|intelligence|js|marketing|newsletters|scripts|__includes|clients|specific_scripts) - [L]

This rule stops processing URLs that match it, including /CMS (although it's upper case so it probably wouldn't affect it).

Code: Select all
RewriteRule ^(.*)_(.*)$ /index.php?tag=$1&page=$2/ [L]

This rule matches /cms/free-demo_16.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^cms/(.+)_([0-9]+)$ /cms/index.php?tag=$1&page=$2/ [L]

RewriteRule ^(images|CMS|cmsm|downloads|documents|intelligence|js|marketing|newsletters|scripts|__includes|clients|specific_scripts) - [L]
RewriteRule ^(.+)_([0-9]+)$ /index.php?tag=$1&page=$2/ [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

No Luck

Postby anawaz » Mon Jul 20, 2009 1:52 am

Thanks, Richard, but that didn't do the trick. Any other suggestions?
anawaz
 
Posts: 7
Joined: Wed Jul 15, 2009 1:49 am
Location: London

Postby richardk » Mon Jul 20, 2009 4:23 pm

What happens? Does it (/cms/free-demo_16) match the last rule?
Do you have a .htaccess file in /cms?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^cms/([^/]+)_([0-9]+)$ /cms/index.php?tag=$1&page=$2/ [L]

RewriteRule ^(images|CMS|cmsm|downloads|documents|intelligence|js|marketing|newsletters|scripts|__includes|clients|specific_scripts) - [L]
RewriteRule ^([^/]+)_([0-9]+)$ /index.php?tag=$1&page=$2/ [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

It works!

Postby anawaz » Mon Jul 20, 2009 10:56 pm

Your code was fine, I was being daft.

You see when I had the first rule on on the base URL it also redirected the common pages, for instance cms/free-demo_16, but I'd manually put in redirects to redirect index.php?page=16 to whaterver-went-here_16 on the base URL and to avoid it from applying to CMS had blocked the directory out there.

So, it was basically ignoring the rule and 404ing on me.

Thanks, Richard.

You are the man!
anawaz
 
Posts: 7
Joined: Wed Jul 15, 2009 1:49 am
Location: London

More complications with subdirectories

Postby anawaz » Wed Jul 29, 2009 3:38 am

Hi Richard, now I have another issue.

This is what my .htaccess has for now:

Code: Select all
Options +FollowSymLinks

Rewriterule ^(cms/images|cms/CMS|cms/documents|cms/js|cms/scripts|cms/__includes|cms/specific_scripts) - [L]
RewriteRule ^cms/([^/]+)_([0-9]+)$ /cms/index.php?tag=$1&page=$2/ [L]

Rewriterule ^(newsletters|images|CMS|cmsm|downloads|documents|intelligence|js|marketing|scripts|__includes|clients|specific_scripts) - [L]
RewriteRule ^(.*)_(.*)$ /index.php?tag=$1&page=$2/ [L]


Now I have physical files sitting on the server in then newsletters directory, on, for instance:

http://www.vafta.com/newsletters/2009/j ... es/123.jpg

It resolves the actual URL http://www.vafta.com/newsletters/2009/july/ just fine, but it doesn't display any of the images in the july/images directory! When I try to view the actual image, it tries to render the home page as you can see here: http://www.vafta.com/newsletters/2009/j ... on_img.jpg

Why is this happening when it already been told to avoid anything with newsletters in the URL. I've tried adding newsletters/2009 and variations thereof, but no luck.

Thanks for your help in advance!
anawaz
 
Posts: 7
Joined: Wed Jul 15, 2009 1:49 am
Location: London

Next

Return to Content

Who is online

Users browsing this forum: No registered users and 4 guests

cron