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]
Related
In the first place, I want to create a new homepage URL. So instead of www.example.com my new homepage (base) should be www.example.com/abc.
So no matter what the user types as an URL, for example www.example.com/blabla, we need to redirect him to www.example.com/abc/blablabla.
If I am correct, the following will do that:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/abc.*
Rewriterule ^(.*)$ /abc/$1 [R=301,L]
Now comes the problem.
My new base isn't abc, but 🌐. The URL valid value of this 'globe' is %F0%9F%8C%90. So you would think it's easy and replace all abc's in the .htaccess snippet above and your done, but unfortunately that isnt't the case.
Hope someone can help me out.
Regards,
TK
UPDATE:
Going to www.example.com results in loop that looks like www.example.nl/%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0%25F0FC0.
Notice that besides the loop, the icon is not showed as well..
In my example the .htaccess looks like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/%F0%9F%8C%90.*
Rewriterule ^(.*)$ /%F0%9F%8C%90$1 [R=301,L]
UPDATE 2:
Ok, I fixed the icon part by adding NE as an flag and backslashing the unicode characters..
RewriteCond %{REQUEST_URI} !^/%F0%9F%8C%90.*
Rewriterule ^(.*)$ /\%F0\%9F\%8C\%90/$1 [NE,R=301,L]
So:
www.example.com
now results in
www.example.com/🌐/🌐/🌐/🌐/🌐/🌐/🌐/.....
But still got the loop.. :(... anyone got an idea?
ANSWER:
Just replace abc with the actual icon 🌐. Make sure you do this using an editor that can handle these unicode icons... PhpStorm can't, but TextMate or Sublime works just fine.
Notepad++ works fine, too ^^ maybe it helps.
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
I have the following entry in my .htaccess file:
RewriteRule ^blogs$ ?name=data&case=gview&group_id=31%1 [L]
What I do is, I redirect blogs to ?name=data&case=gview&group_id=31
Now what happens is, all my urls are now blogs?id=1 etc, but I need them to stay just ?id=1
How can I remove the blogs from rest of the urls?
This what I came up with, but it doesn't work:
RewriteRule ^blogs?(.*)$ /$1 [L]
EDIT I might be explaining it wrong. I need to change the actual url display of the links. Is that actually possible?
Not sure... but is that what you're looking for ?
RewriteRule ^/?$ /blogs [L,QSA]
What's wrong with just adding a R to your original rule?
RewriteRule ^blogs$ ?name=data&case=gview&group_id=31%1 [L,R=301]
Ok, the answer is pretty much: It can't be done. I just went over my code, added / before main urls and all is working as intended.
Thank you both for suggestions.
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]
I am trying to write a rule which says
xyz.com/hotels_in_someplace will direct to xyz.com/test.php?place=someplace
by using the following in my htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^hotels_in_([a-z]+)$ test.php?place=$1 [L]
but its not working and I cant figure out why, I am running a wamp server as a dev. when i tried to run xyz.com/test.php?place=someplace (without the htaccess in the directory) it works but I suspect there's something my rule which is wrong.
additional:
whoops....my dumb ass mistake, mod_rewrite wasnt enabled...
Try it without the end-line anchor ($)
RewriteRule ^/hotels_in_([a-z]+) /test.php?place=$1 [L]
Also, did you allow the .htaccess files to be parsed?
Update: I didn't notice the RewriteBase in the question before so my point about the leading slash is null. However the RewriteRule does work on my machine.
Try:
RewriteRule ^hotels_in_([a-z]+)$ /test.php?place=$1 [L]
The path to test.php needs the leading slash. The way you are doing the rewrite you will be redirected to xyz.com/path/to/web/docs/on/machine/test.php and thats not what you want. :)
Also if you are you including a trailing slash in your test.
The above rule will not match xyz.com/hotels_in_someplace/.
To match the leading slash this should work:
RewriteRule ^hotels_in_([a-z]+)/?$ /test.php?place=$1 [L]
Your example works fine for me. Might want to make a few little adjustments (see my example, which captures uppercase letters, hyphens and underscores (for hotels in countries with more than one word (united-states, united_states)) and a trailing forward slash).
RewriteRule ^hotels_in_([A-Za-z_-]*)(/?)$ /test.php?place=$1
Hope that helps.