Htaccess Redirect Rewrite Not working together - php

I want to remove the query strings from the urls using Htaccess, I used the following code for changing the urls, It did but after redirection to that url, I am getting 404 error.
and there is rewrite statement also if i use that only, then the new url works without 404 error, but the old urls doesnt get automatically redirected to new urls.
Here is the htaccess and url the I am modifying
Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/profile\.php$
RewriteCond %{QUERY_STRING} ^user_id=([0-9]*)$
RewriteRule ^(.*)$ http://www.meenmipage.com/user/%1? [R=302,L,NC]
Actual Url was:
http://www.meenmipage.com/profile.php?user_id=2
and modified was:
http://www.meenmipage.com/user/2
If i remove the above code and just use the rewrite statement as like this one:
RewriteRule ^user/([^/]*)$ /profile.php?user_id=$1 [NC,L]
Then the new modified url works and the old one also works
Please tell me what to do?

I think you need to remove $ at the end of this line:
RewriteCond %{REQUEST_URI} ^/profile\.php
because if the request URI has user_id=2 then this condition will not match

Try This !
-----------
Options -MultiViews
Options +FollowSymlinks
RewriteEngine on
rewritecond %{QUERY_STRING} ^user_id=([0-9]*)
rewritecond %{http_host} ^www.meenmipage.com [nc]
RewriteRule ^([0-9]+)$ //www.www.meenmipage.com?user_id=$1 [L,QSA]

Related

How to rewrite an url with two query inside to one folder?

I'm facing a problem to transform my actual url
http://website.com/login/profil.php?id=34&pseudo=robin
into this one :
http://website.com/myspace
I checked if the rewrite engine works well and it's ok so that's my .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %{QUERY_STRING} ^&pseudo=([a-zA-Z0-9]+)$
RewriteRule ^myspace\$ http://website/login/profil.php?id=$1&pseudo=$2 [R=301,L]
So my wish is to redirect the user's space on one common directory. what's the best way do do this ? am i wrong with the query ?
Thanks in advance guys !
This code doesn't work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^test\.html$ /profil.php?id=([0-9]+)&pseudo=([a-z]+) [L]
Try this .htaccess it will work only when get parameters are in the same order i.e.
id=32&pseudo=code
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/login/profil\.php$
RewriteCond %{QUERY_STRING} ^id=(\d+) [NC]
RewriteCond %{QUERY_STRING} &pseudo=(\w+) [NC]
RewriteRule ^login/profil\.php$ /myspace? [L,R=301]
This format works for me. You also try this:
Put this line into your .htaccess file
RewriteRule ^myspace/?$ login/profil.php?id=34&pseudo=robin
And yes, do not forget to set rewrite base in your .htaccess file:
below line will set the base url:
RewriteBase /
So, basically you need to put these both lines into your htaccess file
RewriteBase /
RewriteRule ^myspace/?$ login/profil.php?id=34&pseudo=robin
For dynamic id and name, you can use:
RewriteRule ^myspace/([0-9]+)/([A-Za-z0-9._-]+)/?$ login/profil.php?id=$1&pseudo=$2
So, into your profile.php file, you can check parameter values(id and name)by
print_r($_REQUEST);
So, example, you will have to run the url something like,
http://websitename.com/myspace/20/robin/ or
http://websitename.com/myspace/25/john/ ... and like wise.

php .htaccess create subdomain excluding query string

I have following records in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(^.*)\.example\.com
RewriteRule ^([0-9].*)$ /folder/%1/$1 [L,NC,QSA]
What I want is:
Show example.com/folder/user/0's content by accessing user.example.com/0
When I access user.example.com/0 my link looks like: user.example.com/folder/user/0
How can I fix it?
Here's something that would take any URL of the form user.example.com/N and make it into a redirect to example.com/folder/user/N
RewriteCond %{HTTP_HOST} ^(^.*)\.example\.com
RewriteRule ^([0-9]+)$ http://example.com/folder/%1/$1 [L,NC,QSA]
The redirect happens because of the change of host. This means the user's browser bar will become example.com/folder/user/N

Redirect URL as well as Change Query String Apache

I have two conditions:
http://localhost/restexample/api/con/2/
rewrites to
http://localhost/restexample/RestController.php?phone=single&id=2
and
http://localhost/restexample/api?number=12345
redirects/rewrites to
http://localhost/restexample/RestController.php?phone=all&no=12345
I am getting proper response in 1st case but not in second case.
My .htaccess file is:
# Turn rewrite engine on
Options +FollowSymlinks
RewriteEngine on
# map neat URL to internal URL
RewriteRule api/con/([0-9]+)/$ "RestController.php?phone=single&id=$1" [nc,qsa]
RewriteCond %{QUERY_STRING} number=(\d+)$
RewriteRule "^api" "RestController.php?phone=all&no=%1" [nc,qsa,R=301]
Somebody please help.
Output of second file:
Try these rules:
Options +FollowSymlinks
RewriteEngine on
# map neat URL to internal URL
RewriteRule ^api/con/([0-9]+)/?$ RestController.php?phone=single&id=$1 [NC,L,QSA]
RewriteCond %{QUERY_STRING} (?:^|&)number=(\d+)$
RewriteRule ^api/?$ RestController.php?phone=all&no=%1 [NC,L]

.htaccess rule gives 500 error for php file

I saw another stackoverflow answer to come up with this current setup.
I want requests to http://myserver.com/book/1234 to actually show the content from php file http://myserver.com/book.php?id=1234
So my .htaccess looks like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^book/(.*)$ /book.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Notice the bottom two lines allow me to use url's like http://myserver.php/login to actually pull up http://myserver.php/login.php This is working correctly.
But I get a 500 Error when I try calling http://myserver.com/book/1234 What is wrong?
Your code is instructing the server to only do the rewrite if the query string is present. As a result, the rule would only work if you requested /book/1234?id=1234.
Removing the line below will allow your rewrite to work.
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$

Htaccess new line crashes page

I recently tried to hide parameters from my url, using htaccess, ended up with a 500 Server Error.
Here is my .htaccess code:
<files .htaccess>
order allow,deny
deny from all
</files>
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteBase /
RewriteRule ^([^/]+\.php)/.* $1 [R=301,L]
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]
All I wanted to do is my page buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7 to show always as buyit.php keeping my variables intact and usuable for my php scripts. So I added this last line in htaccess but unfortunately it crashes.
Any ideas why, please?
Your code is looping. This is because you can't match the query string in a rewrite rule, thus a URL like:
/buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7
actually matches:
^buyit\.php$
So you need to add a condition to check that there isn't a query string. Change your last rule to this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]
Unfortunately, if you are passing values using GET method, you cant simply get rid off the parameters. However, you can remove the parameters name and make the url unique and NOT EASILY UNDERSTANDABLE by some one who doesn't know about the parameter names.
Try this htaccess code.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /buyit.php?qsta=$1&gid=$2&qid=$3&kaq=$4&req=$5&dlk=$6 [L]
It converts your URL from this :
http://yourdomain.com/buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7
to this
http://ydomain.com/1/11-64-36/4/0/14/7.html
which is also good for your SEO.

Categories