Rewriting URL rule, example like StackOverflow - php

What would be the apache rewrite url to extract the id number (434376) from this url?
http://stackoverflow.com/questions/434376/unique-url-slug
something like
RewriteEngine On
RewriteRule ^questions/([0-9]+)/?$([A-Za-z0-9-]+) post.php?post_id=$1 [NC,L]
...but it doesnt work on my site...any tips?
thanks

Tested:
RewriteRule ^questions/([0-9]+)/?.*$ rewrite.php?post_id=$1 [NC,L]

Well if the / is optional it probably should be:
RewriteRule ^questions/([0-9]+)/?([A-Za-z0-9-]+)?/?$ post.php?post_id=$1 [NC,L]
Let me know if it works..

Related

Rewrite mods with two GET variables

I need to change url from mydomain.com/search.php?page=1&postcode=xxx
to: mydomain.com/search/1/xxx
I know how to change url from mydomain.com/search.php?page=1 to mydomain.com/search/1
This can be done with this:
RewriteRule ^search/([0-9]+)/?$ search.php?page=$1 [NC,L]
How can I add another GET to my url?
Thank you!
This should work:
RewriteRule ^search/([0-9]+)/(.*)$ search.php?page=$1&postcode=$2 [NC,L]

Rewrite rule - strange behaviour with $_GET

I have this rewire rule: EDITED
RewriteRule ^(.*)$ $1.php [NC]// the problem is caused by this. How can I keep it without having this result?
RewriteRule ^user/(.*)$ user.php?user=$1 [NC]
RewriteRule ^ride/(.+)/$ ride.php?myRideId=$1 [NC,L]
when I do this:
www.example.com/ride/123
everything works fine and my browser correctly shows the above page but if I try to use:
$rideId = $_GET['myRideId'];
echo $rideId;
the result is:
123.php/123
I don't understand why. Anything wrong in the rewrite rule?
Have you tried doing this?
RewriteRule ^ride/(.+)/?$ ride.php?myRideId=$1 [NC,L]
Try this. Also you will need to write your URL as:
www.example.com/ride/123/

mod rewrite, htaccess and gettign the values

the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies

htaccess dynamic query string

maybe this is a stupid question
i have current code below:
RewriteEngine On
RewriteRule ^(.*) /image/modify.php?pic=$1 [QSA]
and i enter the url http://site.com/image/20121207/test.jpg&w=256
will display the resized image.the original url is:
http://site.com/image/modify.php?pic=20121207/test.jpg&w=256
but how can i change &w= to ?w= ?
the w= are random,perhaps someone will enter h= / r= ...etc
please give me some hint
The Apache rewriter is intelligent enough to process query parameters correctly. That's what the QSA is for, "query string append". You can access 20121207/test.jpg?w=256 and it should be rewritten to modify.php?pic=20121207/test.jpg&w=256 correctly.
Try this:
RewriteRule ^([^/]+)/([^/]+)/([^?&]+)?(.*) /image/modify.php?pic=$1$2$3&$4 [QSA]
So easy :
RewriteEngine On
RewriteRule ^([^/]*)(?)(.*)$ /image/modify.php?pic=$1&$2 [QSA]
Hope it work ....
How about this:
RewriteEngine On
RewriteRule ^image/([^/]*)(?)(.*)$ /image/modify.php?pic=$1&$2 [QSA]
and enter the url:
http://site.com/image/20121207/test.jpg?w=256
i has try it, i think it will work ...
Good luck

.htaccess clean url with more than 3 paramenters

I am new to .htaccess and I seem to be having some problems getting it to clean up the url for me.
I have
site.com?p=article&id=3&read=article title
I am able to get the first variable to work ok like site.com/articles. but when I try to go further the server is saying it cant find it. I have tried several methods none of which seem to be working.
RewriteRule ^([a-zA-Z0-9]+)$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?p=$1
this above is working
I have tried
RewriteRule ^([a-zA-Z0-9-z\-]+)(/([a-zA-Z0-9-z\-]+))?/?$ index.php?p=$1&i=$2
and
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?p=$1&i=$2&read=$3
the last 2 are not working. help please. Thanks
Let's try something like this?
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&read=$3 [L]
%{QUERY_STRING} is what you are looking for.
this is what I use for a similar situation:
RewriteRule ^/(.*) /index.php?r=/$1&%{QUERY_STRING} [L]

Categories