Apache Rewrite keeping the original QueryString on the Rewrite URL - apache

I have a problem.
The follow code is my Apache configuration:
RewriteRule ^/page-(.*)$ https://%{SERVER_NAME}:%{SERVER_PORT}/site/spaces/portal/page/$1 [QSA]
So, its OK. But, when I access my page like:
www.mysite.com/page-page?query=blah
It redirects the URL without the query=blah params.
What I'm doing wrong?
Thanks

I am not sure why they are missing, but you should be able to put them back with %{QUER­Y_S­TRING}.

Related

Redirecting a URL that contains a specific part to another URL using htaccess

I would like to know if there is a way to redirect a URL that looks like this:
https://www.example.com/p=d3d82c7c
to
https://www.example.com/polls/poll?poll=d3d82c7c
using htaccess.
What I want to do is to create a more simple link for the users. Is that possible with htaccess only?
PS: the code at the end may change within each URL.
Thank you very much.
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^.*=(.*) polls/poll?poll=$1 [L]
OR with your shown samples if your URI always starts with p then you could try following, but make sure either previous OR this only one of them should be used at a time.
RewriteEngine ON
RewriteRule ^p.*=(.*) polls/poll?poll=$1 [L]
You can use this method:
Redirect 301 /en/php/project.html http://www.example.org/newpage.html

Apache Mod_Rewrite - Redirect losing POST data - 405 error

Stackoverflow users,
I have an Apache application that needs to accept data POSTed to the following paths:
/sample/HostChange/Submit
/sample/HostChange/SubmittoAPI
I'm currently using the following 301 redirect rules. This is not what I want as as the POST gets redirected and the second request is a GET loosing all the data. I am seeing the 301 request go the correct url but the second request is a GET and causes a 405 response code.
.htaccess:
RewriteEngine On
Redirect 301 /sample/HostChange/Submit /event
Redirect 301 /sample/HostChange/SubmittoAPI /date
I'm sure using a Redirect is the issue. Can someone help me figure out the correct RewriteCondition I need to be using to redirect these POST hits to the new paths but keep the data being submitted to the application.
Thank you mucho.
I don't think you can redirect a POST (as a POST). Browsers just won't POST the data again.
You'd have to output some HTML with Javascript to make the browser rePOST the data to the new URL.
Or instead of redirecting, have some server-side code accept the POST data first and then dispatch it somehow (maybe be redirecting with an indentifying token in the URL) internally.
Or if the data is short, re-write the URL to include the data as query parameters.
Something along the lines of following should work, I'm not around an apache instance at the moment so regex is not tried but please check the rewrite log and see how it behaves.
RewriteEngine On
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 9
RewriteCond %{REQUEST_URI} /sample/HostChange/Submit
RewriteRule ^/sample/HostChange/Submit(.*) /event/$1 [P,L]
I some kind of the same problem, for me it helped not to really redirect the request, but to rewrite it. I don't now if this is applicable to your problem.
Here are the details and it worked for me:
PHP Rewrite url and preserve posted data

Redirect to new URL`s

i want Redirect the old url`s to new address.
Redirect all
http://www.mysite.com/viewdownload/**/**
url`s to
http://www.mysite.com/download/viewdownload/**/**
example:
http://www.mysite.com/viewdownload/21/323
Should be Redirect to
http://www.mysite.com/download/viewdownload/21/323
or
http://www.mysite.com/viewdownload/13/961
Should be Redirect to
http://www.mysite.com/download/viewdownload/13/961
RewriteRule ^(viewdownload)/(\d+)/(\d+)/?$ download/$1/$2/$3 [L,R=302,QSA]
If this does what you'd like it to after some testing, change R=302 to R=301.
If you are using Apache this is something for mod_rewrite / url rewriting & not Joomla.
You got the wrong keywords. Search for mod_rewrite or come back if you need more help.

htaccess rewrite rules are not working with urls that end with .cfm

I'm working on fixing all my URL's to be shorter with 301 redirects. I have fix almost all of them, however there is a url that is ending with .cfm that will not rewrite.
FROM: http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
TO: http://www.mydomain.com/story/resources/health/page/168/page.html
If I change /page.cfm to /page.html then the rewrite will work.
Here is the rewrite rule that works for my other urls
RewriteRule ^index.cfm/catlink/([a-zA-Z0-9/-]+)([/])pagelink/([a-zA-Z0-9/-]+)([/])sublink/([a-zA-Z0-9/-]+)([/])art/([a-zA-Z0-9/-]+)(.*)$
http://localhost/index.cfm?page=moved&cat=$3&subcat=$5&article=$7&story=$8 [R=301]
Why does it work when the URL ends with .html but not when it ends with .cfm? What am I doing wrong?
This is current link and will not work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
If I manually change the end of it to .html, I can get it to work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.html
The issue is that Apache httpd is passing it off to Tomcat before Apache looks at the .htaccess. To test this, move your rewrite rules into your vhost. If they work, then that's what the problem was.
First off, change your the first part of your RewriteRule to be the following, more concise expression:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/(.*)$
I believe that alone might resolve the issue. However, if it does not, and you don't care about the rest of the URL, try the following:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/
Note: this removes the anchor ($) and therefore allows the URL to be open ended.

Return a 404 for all requests

I have a development version of a website that I want to hide/disable. But I don't want to delete the files for the moment. I also don't want to redirect requests to somewhere else. I just want to respond to the requests for that website with a HTTP 404.
How should I do it?
I am using Apache and .htaccess.
You could do something like this:
RewriteEngine on
RewriteRule ^oldsite/?$ http://www.domain.com/errorpage.html [r=301,nc]
This is a little gentler than a hard 404. Otherwise you can change the response code. Hope this helps.