Send full url with query string to file in mod_rewrite - php

I want to catch every request that comes in on my domain name and redirect that url to a php file. In this PHP file I show a form and when the user passes the form I want to redirect the user to the page it requested orignally.
I've got the following mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !(accepts_cookie) [NC]
RewriteRule ^(.*) test.php?link=.. [L]
As you see I would like to place the full requested url in the link= but how am I able to do this? I tried stuff like: REQUEST_URI, but that doesn't give me the full path including query strings.
I hope someone can help!

Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_COOKIE} !(accepts_cookie) [NC]
RewriteRule ^(.*)$ test.php?link=%{REQUEST_URI}%{QUERY_STRING} [L]
</IfModule>
You can always consider the mod_rewrite Cheat Sheet.

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.

How to rewrite URL and return `404` for original links?

I created my website with some files in htdocs folder, such as: .htaccess, web.php ... In past, I used an old URL structure for my website: old.php?id=12. Then, I have used a new one: new/12, by using this simple .htaccess:
RewriteEngine On
RewriteRule "^new/([0-9]+)/?$" "/old.php?$1"
And now, I do not want any user can access my website with old URL structure. Could you show me: How to rewrite URL and return 404 for original links?
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^new/(\d+)*$ ./old.php?id=$1
The best solution, especially for SEO, is to make a redirect 301:
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+old\.php\?id=(\d+) [NC]
RewriteRule ^ /new/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^new/(\d+)/?$ /old.php?id=$1 [L,QSA,NC]
To get a real 404 effortlessly, the simplest is to change your hidden page name to new.php (also in htaccess) and delete the old.php page.

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

Htaccess Redirect Rewrite Not working together

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]

.htaccess: Hide URL variables

I have no idea how to play with .htaccess file to change / hide variables from URL
I want to use .htaccess file of WordPress
my .htaccess file already contains this scripts:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my_site/index.php
[L]
</IfModule>
# END WordPress
I have url
http://localhost/my_site/project-detail/?pro_id=7
and I want to either hide pro_id=7 like
http://localhost/my_site/project-detail/
but could be able to catch this $_GET['pro_id'] in my php script file.
or
http://localhost/my_site/project-detail/7
I also tried
RewriteRule ^project-detail/(.*) /project-detail/?pro_id=$1
Have you tried something like that:
RewriteEngine On
RewriteRule ^project-detail/([0-9]+)$ project-detail/?pro_id=$1
You could try adding these above the RewriteBase /my_site/ directive, as they need to be before your wordpress stuff.
First, you can access the actual request directly to redirect to the URLs without the query string:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /my_site/project-detail/?\?pro_id=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /my_site/project-detail/%1?%2 [R=301,L]
This will make it so a URL like this: http://localhost/my_site/project-detail/?pro_id=7 gets redirected to http://localhost/my_site/project-detail/7 and a URL like http://localhost/my_site/project-detail/?pro_id=7&other=param gets redirected to http://localhost/my_site/project-detail/7?other=param. If you don't want any extra params at all, remove the %2 from the rule's target. This makes it so the browser's address bar changes to the URL without the pro_id query string. Now we have to internally rewrite it back to the query string:
RewriteRule ^my_site/project-detail/([^/]+)$ /my_site/project-detail/?pro_id=$1 [QSA,L]
So internally, the URI gets rewritten back to /my_site/project-detail/?pro_id=7 but the browser's address bar still has the clean looking URL.

Categories