Rewrite rule - strange behaviour with $_GET - php

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/

Related

htaccess multiple parameters rewrite rule

I know this problem is over asked, but couldnt find anything fitting with my problem.
I'm currently creating a website, and my url are like :
www.foo.com/
or www.foo.com/index.php.
They can take 1, 2 ,or three different parameters like
www.foo.com/index.php?page=Home&lang=en&article=1
What i'd like is an url like
www.foo.com/Home/
or www.foo.com/en/Home
or www.foo.com/Article/1
or www.foo.com/en/Article/1
The page parameter is required, other two are not..
I cant have anything working for me... Any help would be greately appreciated
Thanks a lot !
Better to have separate clean rules. Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
RewriteRule ^([a-z]{2})/([^/]+)/?$ /index.php?page=$2&lang=$1 [L,QSA]
RewriteRule ^([a-z]{2})/([^/]+)/([0-9]+)/?$ /index.php?page=$2&lang=$1&article=$3 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/?$ /index.php?page=$1&article=$2 [L,QSA]
Try something like this
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)\.html$ index.php?param1=$1&param2=$2&param3=$3

Rewrite domain, but still use index.php

Current code to use index.php for all urls :
RewriteRule ^(.*)$ ./index.php
But for one special case, I want to make url
http://domain.com/s/this_changes to call actually
http://domain.com/steps/step/this_changes.
I tried adding :
RewriteRule ^./s/$ ./steps/step/$1, but it gives an error 500.
Try this:
RewriteEngine On
RewriteRule ^s/(.+)$ /steps/step/$1 [L]
Another way you could do it is to route all your URIs through index.php, and create a function for each one.
RewriteRule ^s/this_changes$ steps/step/this_changes
if it is that simple or
RewriteRule ^(.*)/(.*)$ steps/$1/$2
And your specific code would work like this
RewriteRule ^s/(.*)/?$ ./steps/step/$1
The (.*) is referenced in your $1 in the second part of your code.

.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]

Rewriting URL rule, example like StackOverflow

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..

mod_rewrite with ? in the original URL like YouTube

Ok I want to simulate the YouTube URL and redirect it to my real URL. Reason being is I used a random string to make video ID's so they aren't guessable by sequence.
I have the links as so
http://www.mysite.com/watch?v=Dxdotx3iT1
and want to redirect to
http://www.mysite.com/index.php?page=videos&section=view&v=Dxdotx3iT1
Can't seem to figure out the mod rewrite. Also using a ? I believe tells it to do something.
Any help would be appreciated.
You need to adjust your RewriteRule to include the query string using the [QSA] (query string attached) flag:
RewriteRule ^watch$ index.php?page=video&section=view [QSA]
RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=videos&section=view&v=%1 [QSA,R=301,L]
None of these answers worked for me, so in the end I found through trial and error the following worked for me;
The RewriteRule to get my URL to look like this https://www.example.com/video/watch?v=UnIq3Id follows;
RewriteRule ^video\/watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/UnIq3Id interfered with my redirecting;
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
The RewriteRule to get my URL to look like this https://www.example.com/watch?v=UnIq3Id is as follows;
RewriteRule ^watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/ interfered with my redirecting;
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Hope it helped someone and saved you the headache I had, people are not so forthcoming on this issue at times How do you write an htaccess RewriteRule to make my urls work like youtube?.

Categories