I've looked online and couldn't find exactly what I was looking. I tried to combine two different tutorials together but still nothing.
I need an .htaccess rule that changes:
game.php?id=$1&title=$2
to
/game/$1/$2.html
I've gotten it to work with one word as $2 but if there is a space, everything crashed. My current code is
RewriteEngine on
RewriteRule ^game/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ game.php?id=$1&title=$2
Any help will be greatly appreciated.
Thanks,
It looks like your wrote the opposite of what you are asking for. Currently your rewrite will rewrite
/game/$1/$2.html to game.php?id=$1&title=$2
RewriteRule ^game/([^/]+)/([^/]+)/?$ /game.php?id=$1&title=$2 [L]
if you are wanting the other way like you asked it'd look like this:
RewriteCond %{QUERY_STRING} ^id=(.*?)&title=(.*?)$
RewriteRule ^game.php$ /game/%1/%2.html? [L]
If it's only about spaces try adding "\s" to square brackets:
RewriteRule ^game/([A-Za-z0-9-\s]+)/([A-Za-z0-9-\s]+)/?$ game.php?id=$1&title=$2
Related
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
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
Can anyone explain to me why this rewrite rule doens't work:
RewriteRule ^architecture/([a-zA-Z_]+)/(.*).html$ web/index.php?field=1&sub=$1&name=$2
on this url: http://localhost/greenlinked1-6.com/architecture/projects/84-test-deeplink-test.html
And what I should do to get it working.
I've tried to find the answer in several articles but I can't figure it out.
Your .htaccess file should be placed in the directory greenlinked1-6.com. Make sure there are no conflicting rules, like this:
RewriteRule ^(.+)$ $1 [L]
# This line will never be be matched
RewriteRule ^architecture/([a-zA-Z_]+)/(.*).html$ web/index.php?field=1&sub=$1&name=$2
check that u havn't used RewriteBase / just remove & try again...
I am trying to rewrite URLs ending like (not only exactly equal to) this:
comments/The-Latest-Out-of-Pakistan/68
into URLs ending in this:
comments/index.php?submissionid=68
Below is what I have in the .htaccess file, but it's not working.
RewriteEngine On
RewriteRule ^comments/([A-Za-z0-9-]+)/([0-9]+)?$ comments/index.php?submissionid=$2 [NC,L]
Any idea why it's not working?
Thanks in advance,
John
I think the only issue is that you've not escaped the hyphen in the character class [A-Za-z0-9-]+, try replacing it with this [A-Za-z0-9\-]+ and see if that works. If not, we can work from there.
I tested it and this one works:
RewriteEngine On
RewriteRule ^comments/([a-zA-Z0-9-]+)/([0-9]+)?$ comments/index.php?id=$2 [NC,L]
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