Problems with mod_rewrite and new url structure - apache

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]

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.

htaccess redirect - too many redirects

I need to rewrite
www.domain.co.uk/store?cat=57
to
www.domain.co.uk/store/57
but I keep getting a 'too many redirects' error. I've got the following in my htaccess:
RewriteCond %{QUERY_STRING} ^cat=([^./]*)$
RewriteRule ^store\.php$ /store/%1? [R=301,L]
RewriteRule ^store/([^./]+)$ store.php?cat=$1 [L]
The redirect works if I comment out the bottom line, however this then stops my pages loading properly.
Any ideas?
Thanks,
Chris
You can use:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+store\?cat=([^\s&]+) [NC]
RewriteRule ^ /store/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^store/([^./]+)/?$ store?cat=$1 [L,QSA,NC]

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]

Apache URL rewrite and redirect

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.