Jump to content

Recommended Posts

I have this rewrite rule

 

RewriteRule ^([^/\.]+)/?$ index.php?open=my&user=$1 [L]

 

This allows my website to have links like this

 

www.site.com/usernamehere

 

Works great, however, if the username as a fullstop in it, I get page cannot be displayed

 

www.site.com/username.here

 

Is there something wrong with the rewrite ^([^/\.]+)/?$

Link to comment
https://forums.phpfreaks.com/topic/113569-solved-mod-rewrite-dot/
Share on other sites

[^/\.]

 

the ^ at the beginning, means anything EXCEPT for the characters listed. are you looking to allow numbers, letters, and periods? try this instead:

^([a-zA-Z\d\.]+)/?$

 

or if you want underscores too, you can shorten it to:

^([\w\.]+)/?$

i am using this on my system and it works fine:

RewriteEngine on
RewriteRule ^([\w.]+)/?$ index.php?open=my&user=$1 [QSA]

 

<?php
  print_r($_GET);
?>

 

URL: http://hostname.com/test.acount produces output of:

Array
(
    [open] => my
    [user] => test.account
)

 

What does the above produce for you?

What version of Apache are you running?

Is this your server or with a hosting service?

Wait, sorry! It did work, but with another problem

 

When I use your code above, my style sheet wil not load. I have no idea why that would be effected?

 

Here is the HTML head of my document

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="http://www.site.com/" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Websitename</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="java/popup_ad.js"></script>
<script type="text/javascript" src="java/global_ad.js"></script>
</head>

 

I hope you can help

it is because anything with a combination of characters/numbers/underscores/periods is considered a username. Since style.css matches that, it thinks it's a username.

 

You have two options:

1) Move the stylesheet into a directory (like css/style.js). This way it has a slash in the path, and won't match the Rewrite

2) Or, you can modify your htaccess to this instead. This will prevent the Rewrite from happening if it matches a real file. This is the way I recommend, but if someone creates an account called style.css, they won't be able to access it (it will just show them your style sheet)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([\w.]+)/?$ index.php?open=my&user=$1 [QSA]

Ah thats makes sense, problem is I have a few files such as .js etc so would have to move them around

 

I dont mind using

 

RewriteCond %{REQUEST_FILENAME} !-f [NC]

RewriteCond %{REQUEST_FILENAME} !-d [NC]

 

But if my web server is very busy and that is processed on every single page, will it add a bit of overhead..

 

Also, just to clear something up

 

Does rewrite stuff work faster if added to APACHE CONF rather than a .htaccess file?

I just read apache manual for this

 

Apache say never use .htaccess unless you have no access to apache conf as for every single page call it has to check for the .htaccess file in the root directory and every single parent directory

 

I guess it adds up, although probably miliseconds still

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.