frontpor
asked on
Simple Mod rewrite condition
Hello,
I need simple condition for this mod rewrite
RewriteEngine On
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
above mod rewrite this url www.mywebsite.com/user/?P=username to www.mywebsite.com/username
what i need is to ignore certain names .. if my url is this www.mywebsite.com/foldername then it also rewrite url to www.mywebsite.com/user/?P=foldername it should not write this url bcz it is folder ..
I need condition to ignore directory names.. i will put directory names in .htaccess or it search whatever is easy for you..
Thanks
I need simple condition for this mod rewrite
RewriteEngine On
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
above mod rewrite this url www.mywebsite.com/user/?P=username to www.mywebsite.com/username
what i need is to ignore certain names .. if my url is this www.mywebsite.com/foldername then it also rewrite url to www.mywebsite.com/user/?P=foldername it should not write this url bcz it is folder ..
I need condition to ignore directory names.. i will put directory names in .htaccess or it search whatever is easy for you..
Thanks
If you really want a condition that checks for directory existence, try this:
RewriteCond $1 !-d
That should only match if the first captured data in the following RewriteRule is not the name of a directory.
RewriteCond $1 !-d
That should only match if the first captured data in the following RewriteRule is not the name of a directory.
ASKER
okay how about this membername.mywebsite.com?
I don't know if mod_rewrite can do that. I don't think it's able to intercept requests for sub-domains.
ASKER
okay. do i need to write each directory name on seperate line like this
RewriteEngine On
RewriteCond $1 !-directory1
RewriteCond $1 !-directory2
RewriteCond $1 !-directory3
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
RewriteEngine On
RewriteCond $1 !-directory1
RewriteCond $1 !-directory2
RewriteCond $1 !-directory3
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
No, the -d flag means "exists and is a directory". The exclamation mark before it means "not".
So $1 !-d should mean "does not exist as a directory".
So $1 !-d should mean "does not exist as a directory".
ASKER
can you show me sample code.. like i did above.. how to add directories
ASKER
i have added this code.. but its not working for me
RewriteEngine On
RewriteCond $1 !-d
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
RewriteEngine On
RewriteCond $1 !-d
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
Sorry, I've just tested the code on my machine, and I think I've got it now.
I wasn't sure about the path of the directory. Turns out you have to specify it fully.
If you were working in the /path/ directory on your webspace, then this is the code you need:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/path/$1 !-d
RewriteRule ^(.*)$ index.html [R]
The %{DOCUMENT_ROOT}/path/$1 then expands to create a path on your machine which Apache checks to see if it's a directory. If it's not, then the RewriteRule executes. If it is a directory, then Apache looks in that directory instead. Works for me, but let me know if you have trouble.
I wasn't sure about the path of the directory. Turns out you have to specify it fully.
If you were working in the /path/ directory on your webspace, then this is the code you need:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/path/$1 !-d
RewriteRule ^(.*)$ index.html [R]
The %{DOCUMENT_ROOT}/path/$1 then expands to create a path on your machine which Apache checks to see if it's a directory. If it's not, then the RewriteRule executes. If it is a directory, then Apache looks in that directory instead. Works for me, but let me know if you have trouble.
ASKER
hello
i am getting this error.. when i try to access website or directory..
The page isn't redirecting properly
i see this url in address bar if i enter into directory.. same www.sitename/directory
http://www.sitename.com/home/username/public_html/index.html
if I open only website then i get this The page isn't redirecting properly
i tried to add RewriteRule ^(.*)$ /directory [R] with slah and without slash.. but its not working for me..
i am getting this error.. when i try to access website or directory..
The page isn't redirecting properly
i see this url in address bar if i enter into directory.. same www.sitename/directory
http://www.sitename.com/home/username/public_html/index.html
if I open only website then i get this The page isn't redirecting properly
i tried to add RewriteRule ^(.*)$ /directory [R] with slah and without slash.. but its not working for me..
Using the RewriteCond in my last post, your full config should look like this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/path/$1 !-d
RewriteRule ^([^/]*)/?$ http://www.sitename.com/user/?P=$1
which should cause exactly the behaviour you specify in your original question.
However, I'm not sure why you've got a query string without a filename. It's probably better to use something explicit like /user/script.php?P=$1 so that you name the script that will deal with the username provided.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/path/$1 !-d
RewriteRule ^([^/]*)/?$ http://www.sitename.com/user/?P=$1
which should cause exactly the behaviour you specify in your original question.
However, I'm not sure why you've got a query string without a filename. It's probably better to use something explicit like /user/script.php?P=$1 so that you name the script that will deal with the username provided.
ASKER
hello,
now it does not redirect to user
i used this
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/folder1/$ 1 !-d
RewriteCond %{DOCUMENT_ROOT}/folder2/$ 1 !-d
http://www.xxxxxxxx.com/user/?P=$1
and also i tried this one too..
RewriteCond %{DOCUMENT_ROOT}/folder1/$ 1 !-d
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
RewriteCond %{DOCUMENT_ROOT}/folder2/$ 1 !-d
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
in the first one directory browsing works fine but it does redirect user .. in the second it consider all folders as a user.. please check this url http://tinyurl.com/y52jw5
now it does not redirect to user
i used this
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/folder1/$
RewriteCond %{DOCUMENT_ROOT}/folder2/$
http://www.xxxxxxxx.com/user/?P=$1
and also i tried this one too..
RewriteCond %{DOCUMENT_ROOT}/folder1/$
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
RewriteCond %{DOCUMENT_ROOT}/folder2/$
RewriteRule ^([a-z]+)$ user/?P=$1 [NC]
in the first one directory browsing works fine but it does redirect user .. in the second it consider all folders as a user.. please check this url http://tinyurl.com/y52jw5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry.. i was confused. i thought i have to add each directory.. its working great. thanks
ASKER
hello,
just checked and got this error.. The user index.php is not a member of our site. everything is fine except the main page which is on the root
just checked and got this error.. The user index.php is not a member of our site. everything is fine except the main page which is on the root
You could make the username matching pattern more restrictive. Which characters are permitted in a username? If you don't need the period (dot) character, and you can live with only allowing letters, numbers, underscores and hyphens in usernames, then change the pattern to this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ http://www.sitename.com/user/?P=$1
That will no longer match index.php because the period is not permitted in the username pattern. But you'll have to remember that when allowing users to create usernames.
It's problems like this that caused me, in my first post, to suggest using a subdirectory for specifying user names.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ http://www.sitename.com/user/?P=$1
That will no longer match index.php because the period is not permitted in the username pattern. But you'll have to remember that when allowing users to create usernames.
It's problems like this that caused me, in my first post, to suggest using a subdirectory for specifying user names.
ASKER
mostly people like to have short url.. thats why i dont want to add another folder wit h username. i will check your rewrite rule. thanks again.
ASKER
thank you. it really works great. You are superb in writing htaccess mods
Glad to be of some use. Good luck with the site.
RewriteEngine On
RewriteRule ^user/(.*)$ http://www.mywebsite.com/user?P=$1 [NC]