Welcome to WebmasterWorld Guest from 35.172.195.49

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

triple RewriteRules

.htaccess problem.

     
11:54 pm on Mar 4, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


Hello Forum,

I have now, for a long time, been trying to put together my .htaccess to suit my shortlink script. Anyway, after googling for hours, read through the forums and fighting. I still can't manage to get it right.

I have put a PHP script in my index file which made my links become smaller and more stylish. (/one/two/three instead of /view_event.php?id=500). However, depending on which page it is there is a diffrent number of folders in the provided URL. Meant, sometimes it can be /one/two and sometimes /one/two/three. Which means that I must have more than a RewriteRule in my. Htaccess.

I have tried and tried. But I cant get it to work. If someone had been kind and gently cloud either decribe or fixed to my code so that it could work?

I would be forever grateful.

This is my code right now: (.htaccess)

RewriteEngine On

RewriteCond %{HTTP_HOST} !^login\.site\.se\/data$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)\/(.*)\/$ ?arg1=$1&arg2=$2 [L]
RewriteRule ^(.*)\/(.*)\/(.*)\/$ ?arg1=$1&arg2=$2&arg3=$3 [L]
RewriteRule ^(.*)\/(.*)\/(.*)\/(.*)\/$ ?arg1=$1&arg2=$2&arg3=$4
3:12 am on Mar 5, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 5, 2012
posts:2
votes: 0


Store no. of pages in database with their corresponding values as:
1-one,2-two,3-three...........

& then Try adding a mumerical approach to rewrite rule after taking word value of a particular id and then redirecting to ur desired page with passing of that variable
4:29 am on Mar 5, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15937
votes: 889


That noise you heard was assorted people-- I name no names-- screaming at the sight of FOUR groups of

(.*)\/(.*)\/(.*)\/(.*)

Luckily your server will never get that far. The first Rule will match all requests that contain at least two layers of directories.

When you have figured out why this is true, we can continue. Hint: There are something like 90,000 Forums threads explaining the problem. At least 20,000 of them show the correct wording instead.

Oh, and you don't need to escape slashes in mod_rewrite. It isn't actively harmful, it just isn't necessary.
10:28 am on Mar 5, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


It isn't me who originally made the script and should only be the one to fix it when a problem came up, which in this case was the .htaccess file. Anyway, my knowledge in apache are extremely limited and therefore very hard for me to understand.

However, I understand partly why it wouldn't work. The first rule will always apply if we do not for example put (. '- [S = n])

I made a new code after reading some of the forums threads and came up with this. Unfortunately, it still doesn't work.

RewriteEngine On

RewriteCond $1 \.(gif¦jpg¦css)$ [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) - [S=3]

RewriteRule ^(.*)\/(.*)\/$ ?arg1=$1&arg2=$2 [L]
RewriteRule ^(.*)\/(.*)\/(.*)\/$ ?arg1=$1&arg2=$2&arg3=$3 [L]
RewriteRule ^(.*)\/(.*)\/(.*)\/(.*)\/(.*)\/$ ?arg1=$1&arg2=$2&arg3=$3&arg4=$4 [L]
1:38 pm on Mar 5, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


(.*) is absolutely the worst pattern you can ever use.

The first (.*) matches the whole input string into $1 leaving nothing to put in $2.

After multiple "back off and retry" trial match attempts, $2 is eventually filled with the last chunk of the URL however long that is. None of the other rules ever get to run.

(.*) is greedy, promiscuous and ambiguous. Don't ever use it unless it is the LAST element of a pattern.

Use a better pattern, like ([^/]+)/ or similar.

Add a leading slash to the rule target.

Do not escape slashes.

The [S=3] code is the wrong way to do this.
5:35 pm on Mar 5, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


Ok. The reason i used the [S=n] was because I earlier saw it in another thread on the forum. [webmasterworld.com...]

However, I tryed and it still didn't work. After reading your replay i changed a few things after your suggestions. Unfortunately, the only thing I get, is an 404 error page.

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?example.se [NC]
RewriteRule ^ - [S=3]

RewriteRule ^([^/]+)/([^/]+)/$ ?arg1=$1&arg2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ ?arg1=$1&arg2=$2&arg3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ ?arg1=$1&arg3=$3&arg4=$4 [L]
6:23 pm on Mar 5, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Before the ? add /index.php or whatever the file is actually called.

Ensure the code is in the root htaccess file.

Be aware that the HTTP specs specify that a URL ending with a slash is for a folder or the index page in a folder. URLs for pages do not end with a slash.
7:10 pm on Mar 5, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


I tested around a little and worked it out at last.

Thank you all for your help. Appreciate it.

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(site\.)?example.se [NC]
RewriteRule ^ - [S=3]

RewriteRule ^([^/]+)/([^/]+)/$ ?arg1=$1&arg2=$2 [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ ?arg1=$1&arg2=$2&arg3=$3 [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ ?arg1=$1&arg2=$2&arg3=$3&arg4=$4 [L]
7:14 pm on Mar 5, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Do you really need the [S=3] code?

"If the request isn't for my site, skip these three rules...".

If the request isn't for your site, this htaccess file won't be used at all.


You should however ensure you fix up non-www/www in a separate ruleset before rewriting the incoming request.
7:43 pm on Mar 5, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


It didn't work without the [S=n].

It is a subdomain, therefore there's no www. But it works, however, if there still are leaks and if it is possible to improve it I am open to do that.

Edit; I can se a small problem after all. Time to time it appears to be an error. 404 error page. But after refreshing och waiting for about 15 minutes it works fine again. The problem only appears on subdomains. Any ideas of what that can be?
3:05 am on Mar 6, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15937
votes: 889


It didn't work without the [S=n].

Do you mean that other subdomains are passing through the same htaccess as yours? You're not in a separate directory? Ouch!

Edit; I can se a small problem after all. Time to time it appears to be an error. 404 error page. But after refreshing och waiting for about 15 minutes it works fine again. The problem only appears on subdomains. Any ideas of what that can be?

Sorry-- you did say your tld was .se didn't you? :)

Er, yes, 404 "ain't no such page" is definitely an error. The refresh-plus-wait solution suggests that it's a caching problem. It's best if you give a specific example. And speaking of example, use example.com to make sure nothing gets turned into a link. We need to see what you really wrote.

Is there more than one subdomain involved in all this? You may need to do some work on the directory structure. I mean the physical directories where the files really live, not the URLs that people use to get there.
3:46 pm on Mar 6, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


Do you mean that other subdomains are passing through the same htaccess as yours? You're not in a separate directory? Ouch!


No I dont. Every subdomain have their own htaccess.

Sorry-- you did say your tld was .se didn't you? :)


Ops. Must have been tired when I wrote it. Didn't even notice it when I read it through. Hah.

All subdomains are put in the same folder. And yes there is more than one subdomain involved. example.com/httpdocs/subdomains/site1 (or) site2.

Does this affect the access for the guest? Is this a problem possible to solve?
4:38 pm on Mar 6, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 28, 2012
posts: 54
votes: 0


(.*)\/(.*)\/(.*)\/(.*)

that reminds me of the Moulin-Rouge!
snigger

/stupidity
2:43 am on Mar 7, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15937
votes: 889


Every subdomain have their own htaccess.
...
All subdomains are put in the same folder.

I'm having a hard time reconciling these two. There can only be one htaccess in a folder. Maybe you'd better explain your directory structure. What I'd want to see is something like

Physical directories:
/home 
/subdomain1
/subdomain2
/subdomain3

et cetera, mapping to

User URL:

www.example.com/ (or example.com, one or the other)
subdomain1.example.com/
subdomain2.example.com/
subdomain3.example.com/
et cetera.

Anything that applies to only one subdomain would then go inside that subdomain's folder. The only thing in the outer folder would be things that apply to everyone in the whole domain, like Deny from... directives.

Is all of this happening within a shared hosting environment, so you can't put anything in the config file?
6:08 pm on Mar 13, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 4, 2012
posts: 7
votes: 0


Physical directories:
/home
/subdomain1
/subdomain2
/subdomain3

et cetera, mapping to

User URL:

www.example.com/ (or example.com, one or the other)
subdomain1.example.com/
subdomain2.example.com/
subdomain3.example.com/
et cetera.


It is more or less like this. My map structure looks like this.

/example.com
-/httpdocs
-/.htaccess
-/subdomains
--/site1
--/.htaccess
--/site2
--/.htaccess

And the user views it as

(www.)example.com
site1.example.com
site2.example.com
8:38 pm on Mar 13, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


You'll need rules in the root htaccess to redirect requests for folders like
example.com/subdomain<x>/<anything>
to
subdomain<x>.example.com/<anything>
to get started.