htaccess advice - how do I write this rule? - .htaccess

I'm not going to lie. I suck at writing htaccess rules. I'm ok with regexes though.
Ok, basically, user enters url: www.site.com/page.id or www.site.com/folder/page.id
That should be internally written to: www.site.com/index.php?page=id
I've got the stuff sorted in the index.php, but I just can't seem to hack it with the htaccess.
What do you suggest it should be?
EDIT: business/legal/service-level-agreement.10 and services.5 would need to work, so the rule would need to get the digit after the dot, period.

Try this rule:
RewriteRule ^([^/]+/)?([^/]+)\.(\d+)$ index.php?$2=$3

The simplest way would be:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?page=$1 [L]
Take a look here maybe this can help:
generateit
cooletips
webmaster-toolkit

Related

Modrewrite for pretty url

How can modrewrite be done in this above situation:
Lets say the website us is: www.real-estate.com
Then we have the first mod rewrite:
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So this will rewrite to something similar: www.real-estate.com/florida and it will list all the real estates in florida.
Then we add this rule:
RewriteRule ^([a-zA-Z\-]+)/(.*)+$ details.php?project=$2
This will rewrite to www.real-estates/florida/project-one and it will display details from that project.
But if I access the link for the city like this: www.real-estae.com/florida/ (with slash last) it will jump me to the second mod rewrite rule, taking me to details.php with an empty variable.
What is the correct solution to slove this problem, so that if the users adds a slash after the city it will still display the projects in that city and not the details page with empty var ?
After playing around I found that his works, but I do not know if it is the correct solution:
I replaced this:
RewriteRule ^([a-zA-Z\-]+)/+(.*)$ details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
With this:
RewriteRule ^([a-zA-Z\-]+)/([a-zA-Z\-]+) details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So I have replaced (.*)$ Why does this work and the other way not I do not know.. Maybe someone can explain.
Probably it fixed the immediate problem but your rules might still match more than required due to faulty regex used and not using anchor $.
For your case use these rules:
RewriteEngine On
RewriteBase /
RewriteRule ^[a-zA-Z-]+/([a-zA-Z-]+)/?$ details.php?project=$1 [L,QSA]
RewriteRule ^([a-zA-Z-]+)/? cities.php?city_url=$1 [L,QSA]
Apache mod_rewrite Introduction

htaccess RewriteRule not processing if not all parameters are passed

Hoping someone can help me with this issue...
I have a RewriteRule set up in my htaccess file that looks like this
RewriteRule ^/?find/(.+)/?in/(.+)/(.+)/ /page.html?&type=$1&city=$2&state_abbrev=$3 [L,QSA]
The rewrite works fine if I have a URL that contains all the parameters specified for example: http://www.site.com/find/doctors/in/dallas/tx/
The issue is, I would like to have this rewrite work even if one of the parameters is missing. For example, if I only entered http://www.site.com/find/doctors/ I would still want it to redirect to 'page.html' and just not have the parameters completed. So in that case it would be writing to http://www.site.com/page.html?type=doctors&city=&state_abbrev=. Currently, if I type in a URL that doesn't have all parameters in it the RewriteRule will not work. Any ideas how to fix this issue?
Thanks in advance...
Replace your rule with this:
RewriteRule ^/?find/([^/]+)(?:/in/?([^/]*)/?([^/]*)/?)? /page.html?type=$1&city=$2&state_abbrev=$3 [L,QSA]
There is nothing stopping you from having multiple rewrites. I have many in my .htaccess for different things and many times you have to. Not one rewrite will work for all occasions.
You could try this.
RewriteEngine on
RewriteRule ^find/(.*)/in/(.*)/(.*) page.html?type=$1&city=$2&state_abbrev=$3&%{QUERY_STRING} [L]
RewriteRule ^find/(.*) page.html?type=$1&%{QUERY_STRING} [L]
It will first look for the full URL
http://www.site.com/find/doctors/in/dallas/tx
Then forward it to the first Rewrite rule.
But then if you used this
http://www.site.com/find/doctors
It will skip the first rule and it will match the 2nd rule redirecting to just the partial query string.
Hope that helps but you can make as many rules or conditions you want. And the order in which they come does matter.

How do I bypass an htaccess rule?

I have the following code in my htaccess file:
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ index.php?page=$1&subPage=$2
It works great but I want to be able to take a url such as:
http://mysite.com/admin/somefolder/index.php?action=test
and have it bypass the rewrite rule and behave as it normally would.
I'm not too sure what the regex would be. Any help?
Thanks!
Use a Rewrite condition to limit the application of the rule.
I think this should work:
RewriteCond %{REQUEST_URI} !index.php
Haven't messed with mod rewrite in a long while, so make sure you test.

HTACCESS url rewrite

I am trying to rewrite this url and it just doesn't seem to be working and I am unclear what is going wrong
I want this URL:
www.mysite.com/dvd-123/Studio/studio_name.php?thumbs=yes&order=price
To actually send to:
www.mysite/cat/list_products.php?studio=123&store=dvd&thumbs=yes&order=price
Here is what I have that is sorta working:
RewriteRule ^dvd-([0-9]+)/Studio/(.*)\.php?(.*)$ cat/list_products.php?studio=$1&store=dvd&$3 [L]
The results I am receiving are the same as if i want to:
www.mysite/cat/list_products.php?studio=123&store=dvd
ignoring the:
&thumbs=yes$order=price
Thank you in advance for your help!
From Apache documentation:
The Pattern will not be matched against the query string... To combine a new query string with an old one, use the [QSA] flag.
I haven't tested this, but hopefully it should point you in the right direction.
RewriteRule ^dvd-([0-9]+)/Studio/(.*)\.php$ cat/user_list_products.php?Manname=$1&store=dvd [L,QSA]
You need to escape the slashes.
RewriteRule ^dvd-([0-9]+)\/Studio\/(.*)\.php?(.*)$ cat/user_list_products.php?Manname=$1&store=dvd&supersale=$3 [L]

.htaccess mod-rewrite question

hey there, i have quite some issues with the mode rewrite here is what i use :
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /creatii.php?creatie_thumb=$2&user=$1 [L]
RewriteRule ^([^/]*)/$ /creatiiuser.php?user=$1
i would like this link :
http://creatii.artcrew.ro/creatii.php?creatie_thumb=creatie19&user=dee-dee
to look:
like http://creatii.artcrew.ro/dee-dee/creatie19
well this is fine, it works, no problems with it but i want to make a rule for another link
http://creatii.artcrew.ro/categorii.php?numecat=poetry&numesubcat=satire
to look like
http://creatii.artcrew.ro/poetry/satire
how can i do this? what rules must i use?
currently if i access http://creatii.artcrew.ro/poetry/satire
it access the link : http://creatii.artcrew.ro/creatii.php?creatie_thumb=satire&user=poetry
how can i make both links(the first one and the second one) work?
one more thing, i want this link : http://creatii.artcrew.ro/creatiiuser.php?user=Dan to look like http://creatii.artcrew.ro/Dan or if that does not work http://creatii.artcrew.ro/user/Dan
how can i do that?
can anyone help me?
thanks in advance
Given the URL http://creatii.artcrew.ro/X/Y, how is mod_rewrite supposed to know whether X and Y are creatie_thumb and user values or numecat and numesubcat values?
You need to add something to the URL to differentiate these cases.
For example:
http://creatii.artcrew.ro/user/dee-dee
http://creatii.artcrew.ro/user/dee-dee/creatie19
http://creatii.artcrew.ro/cat/poetry/satire
RewriteEngine On
RewriteRule ^user/([^/]*)/([^/]*)$ /creatii.php?creatie_thumb=$2&user=$1 [L]
RewriteRule ^user/([^/]*)/$ /creatiiuser.php?user=$1 [L]
RewriteRule ^cat/([^/]*)/([^/]*)$ /creatii.php?numecat=$1&numesubcat=$2 [L]
Maybe you're too much generic with your rules and you got conflicts.
Try to map this urls
http://creatii.artcrew.ro/creatii/dee-dee/creatie1
http://creatii.artcrew.ro/categorii/poetry/satire
Starting from those sample you can easily map your urls with no conflict on rules
RewriteEngine On
RewriteRule ^creaati/([^/]*)/([^/]*)$ /creatii.php?creatie_thumb=$2&user=$1 [L]
RewriteRule ^categorii/([^/]*)/([^/]*)$ /categorii.php?numecat=$1&numesubcat=$2 [L]
Do some tries :D

Resources