I'm trying to rewrite some url's. So I have page which is userAction.php and I rewrite it to /login/ like this
RewriteRule ^login/$ /userAction.php [L]
now on that page login I have two forms - Login and Register. When user make registration and he finish with it the page is still userAction.php but with message: userAction.php?action=joined. I want to rewrite this to login?action=joined. Here is what I'm tried but it it redirecting me to 404 error
RewriteRule ^login?action=joined/$ /fun-second/userAction.php?action=joined [L]
So this is the whole .htaccess
RewriteRule ^login/$ /userAction.php [L]
RewriteRule ^login/?action=joined/$ /userAction.php?action=joined [L]
And this is the redirect when registration is successfully
header('Location: login/?action=joined/');
exit;
Note: With normal URL's is working and there is no problems.
You'd use %{QUERY_STRING} variable to match it.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(action=joined)$ [NC]
RewriteRule ^login/$ /userAction.php?%1 [NC,L]
RewriteRule ^login/$ /userAction.php [NC,L]
The order of rules is also important. Alternatively, you can try the following approach:
RewriteEngine On
RewriteRule ^login/$ /userAction.php [QSA,NC,L]
Note the QSA flag above.
Related
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.
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]
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]
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.
I have several rewrite rules in my .htaccess file that all work fine and I want to add a specific case to turn
sinaesthesia.co.uk/category/psoriasis
into
sinaesthesia.co.uk/category.php5?category=psoriasis
So I tried:
RewriteRule ^(.)category/(.)$ /$1category.php5?category=$2 [L]
which doesn't work. I've tried it without capturing the stuff before 'category' because category.php5 is at the root anyway, so it ought to work without that, have tried:
RewriteRule ^category/([a-z]+?)$ /category.php5?category=$1 [L]
with and without capturing the stuff before category, and nothing works - in fact, I generally get a 500 error! Here is the rest of the file:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
#Translate default page to root
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]
Ah: because I have a document called category.php5 and I'm trying to use category/psoriasis, the server tries to resolve that as category.php5/psoriasis, which fails. Fixed it now!