Apache URL rewrite and redirect - apache

I want to rewrite my url to a new path. From:
www.example.com/test.php?name=xxxx&id=xxxx
To:
www.example.com/test-namevalue-idvalue
When I typed www.example.com/test.php?name=xxxx&id=xxxx in browser, it will take a while to redirect to page www.example.com/test-namevalue-idvalue, but browser complains the website is in redirecting loop. So, I am wondering someone has also met this kind of issue before?
Here is the content of my .htaccess file for Apache:
RewriteEngine On
RewriteCond %{REQUEST_URI} test.php$
RewriteCond %{QUERY_STRING} ^name=(.*)&id=([0-9]+)$
RewriteRule ^test\.php$ /test-%1-%2? [R=302,L]
RewriteRule ^test-(.*)-([0-9]+)$ test.php?name=$1&id=$2 [L]

Try matching against the actual request instead:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+test\.php\?name=([^&]+)&id=([0-9]+)
RewriteRule ^ /test-%1-%2? [R=302,L]
RewriteRule ^test-(.*)-([0-9]+)$ test.php?name=$1&id=$2 [L]

I vaguely remember that I would intuitively set the flags of the last RewriteRule to [PT,L] instead of just [L]. Maybe you try that.

Related

how to Rewrite two parameters URL to SEO friendly URL and Redirect to SEO friendly url

This is my page url example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs
These rules helped me to Rewrite this as example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php?pID=$1&name=$2
RewriteRule ^platform/bidProject.php?pID=$1&name=$2 /platform/project-bids/(.*)/(.*)/ [NC,R=301,L]
but the issue is if I visit to the page example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs the url stays same. I want this to be redirected to example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
So I tired this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /platform/bidProject.php?pID=$1&v=$2 [L,QSA]
Seems I am doing something wrong with this because it makes no any affect on this.
I found a solution to redirect using JavaScript but I like to have .htaccess solution because JavaScript can be disable and can be seen in the source code. The basic intention for doing this fails here.
How can I achieve this using .htaccess
My htaccess path is example.com/.htacsess
You can use these Rules
RewriteEngine on
#redirects /platform/bidProject.php. php?pid=val1&name=val2 to /platform/project-bids/val1/val2/
#redirects the old url to the new one
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php\?pID=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /platform/project-bids/%1/%2/? [NC,R,L]
# rewrites or internally maps the new url to the old one
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Or
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^pid=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^platform/bidProject.php$ /platform/project-bids/%1/%2/? [NC,R,L]
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Change R to R=301 (permanent redirect) when you are sure the rule is working ok.

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

Redirection is not working with mod_rewrite in htaccess

I need to redirect few URIs having query string like:
/pages/foo.bar?pageId=123456 to http://some.site/spam/egg/
/pages/foo.bar?pageId=45678 to http://another.site/spaming/egging/
I have this in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=123456$
RewriteRule ^.*$ http://some.site/spam/egg/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/ [R=301,L]
But its not working, showing 404. What am i doing wrong?
You need to move these 2 rules i.e. before all other rules just below RewriteEngine On line as other rules might be overriding this.
(Based on your comments) Your culprit rule is this rule:
RewriteRule . index.php [L]
Which is actually rewriting every request to index.php and changing value of REQUEST_URI variable to /index.php thus causing this condition to fail:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
From your example, you get redirected to
http://some.site/spam/egg/?pageId=123456
http://another.site/spaming/egging/?pageId=45678
You can use your browser developer tools to see the redirection (in the Network tab).
Maybe the query strings in the redirected URL lead to a 404? You can add a ? at the end of your redirection to clear the query string:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/? [R=301,L]

Problems with mod_rewrite and new url structure

I want to make new url structure on my site:
/page.php?id=1 -> /terms-of-service
/page.php?id=2 -> /faq
So i made in htaccess:
RewriteRule ^terms-of-service /page.php?id=1 [L,NC,QSA]
And it works good, when i go mysite.com/terms-of-service i can see my page number 1. But i also want to make a 301 redirect from old addresses to new ones. When i try to make it like this:
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^page.php$ terms-of-service? [L,R=301,NC]
RewriteRule ^terms-of-service /page.php?id=1 [L,NC,QSA]
I got an "Invalid redirect" error in my browser. How can I fix it?
Your rules will cause redirect loop. You need to use %{THE_REQUEST} here. Have it this way:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /page\.php\?id=1\s [NC]
RewriteRule ^ terms-of-service? [L,R=301]
RewriteRule ^terms-of-service/?$ page.php?id=1 [L,NC,QSA]

htaccess for redirect to SSL

For the past few hours (days) I have been having some trouble redirecting a
page to SSL.
My setup is as follows: I have the following .htaccess for an e-commerce site
on Apache 2.2.16 on Debian (all required mods enabled)
RewriteEngine On
RewriteBase /shop
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
all requests are passed to index.php which acts as my controller and includes
other .php files as necessary.
I now want to use HTTPS for the checkout process which is a php script
cleverly called checkout.php
I thought it would be as easy as changing my .htaccess to:
RewriteEngine On
RewriteBase /shop
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_URI} checkout\.php
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
so that checkout.php is not processed by index.php.
Apparently it is not that simple. I could probably do it by using a hardcoded
https link to checkout but I would prefer to do it with mod_rewrite.
If anyone can share some insight into
this it would be really appreciated.
Thanks in advance
There are a few problems. First, the pattern in your first RewriteRule
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
is written incorrectly. $1 isn't meaningful there (it's a capture result, but no capture has happened yet), and also the query string (part of the request after the ?) isn't part of what's matched, as the RewriteRule documentation says.
Second, I think you meant to use REQUEST_URI instead of SERVER_URI.
So I think you want something like this:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/checkout\.php
RewriteRule .* https://localhost/shop/checkout.php [L,R]
RewriteCond %{REQUEST_URI} !^/(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ /index.php?/$1 [L]
A few notes:
You don't need to match or add back in the query string in the first RewriteRule; mod_rewrite will automatically add it back in.
It's conventional to test RewriteCond %{HTTPS} off instead of
RewriteCond %{SERVER_PORT} !443, as #Jon Lin suggests.
You may want to add the QSA flag in your second RewriteRule.