A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 year.htaccess Optional characters? - page 1
User InfoPosts
.htaccess Optional characters?#1
Though .htaccess, I want to redirect /page/var1/var2 to ./page.php?var1=var1&var2=var2. This is very easy BUT I want to make it so /page/var1 also redirects to ./page.php?var1=var1 (without having the var2). The only way I(m doing this is:

RewriteRule page/(.*)$ ./page.php?var1=$1
RewriteRule page/(.*)/(.*)$ ./page.php?var1=$1&var2=$2

As you can see it(s very redundant, and I have a ton of lines like this, so it gets very messy. Any way to define "optional" parts?

posted date: 2009-04-16 14:02:00

Re: .htaccess Optional characters?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-16 14:02:01

Re: .htaccess Optional characters?#3
mod_rewrite is not well suited to such heavy lifting, leave that to your PHP app. RewriteRule page/(.*)$ ./page.php?vars=$1and somewhere near the beginning of page.php: <?php $vars = explode((/(,$_GET[(vars(]);Voila, you have an array of your vars; now you could do some processing there to see what is required/optional for your app and react accordingly.

posted date: 2009-04-16 14:12:00

Re: .htaccess Optional characters?#4
The best approach is to just hand everything over to your PHP script.I wrote an article on how to do friendly URLs, which covers this in more detail.

posted date: 2009-04-16 14:21:00

Re: .htaccess Optional characters?#5
Thank you, so basically, it is not possible to do it with .htaccess?

posted date: 2009-04-16 14:26:00

Re: .htaccess Optional characters?#6
The expression .* matches both var1 and var1/var2 thus the first rule is applied on both.So you have to specify it that the first rule only matches var1. This can be done by replacing . (any character) by [^/] (any character except /). So try this:RewriteRule ^page/([^/]+)$ ./page.php?var1=$1RewriteRule ^page/([^/]+)/([^/]+)$ ./page.php?var1=$1&var2=$2Edit   You can also write this in one rule:RewriteRule ^page/([^/]+)(/([^/]+))?$ ./page.php?var1=$1&var2=$3

posted date: 2009-04-16 14:39:00

Re: .htaccess Optional characters?#7
I think mod_rewrite stops when it finds a match, so as long as I put the two vars one before the one var one, it will be fine, but thanks for the heads up.

posted date: 2009-04-16 17:47:00

Re: .htaccess Optional characters?#8
THANK YOU! This was exactly what I was looking for.

posted date: 2009-04-17 10:12:00

Re: .htaccess Optional characters?#9
It is possible, but as you have already found out, it is not very practical.

posted date: 2009-04-19 10:11:00

Re: .htaccess Optional characters?#10
+1 Thank you for this! It works perfectly :)

posted date: 2012-02-12 09:13:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.