apache redirect with query string - php

I'm trying to do a redirect if a user goes to (the book_id and course_id numbers aren't constant):
www.mydomainname.com/admin_stuff/add_student.php?book_id=20&course_id=30
to a secure link using:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^book_id=([0-9]*)&course_id=([0-9]*)$
RewriteRule add_student.php https://mydomainname.com/admin_stuff/add_student.php?%1 [R=301,L]
where the redirect is placed in an .htaccess file inside of the admin_stuff folder.
While a redirect occurs, it goes to:
https://mydomainname.com/admin_stuff/add_student.php?1
instead of
https://mydomainname.com/admin_stuff/add_student.php?book_id=20&course_id=30
Any help pointing me in the right direction would be appreciated.
Eric

Change your .htaccess code to
RewriteEngine on
RewriteCond %{QUERY_STRING} ^book_id=([0-9]*)&course_id=([0-9]*)$ [NC]
RewriteRule add_student.php https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]
This makes sure that nothing else about the URL gets changed except its protocol.

Related

How can i redirect /?lang=en to /en?

I'm having trouble redirecting a multilingual website one page. My redirection to htaccess doesn't operate properly. I need assistance with how this can be done. In my htaccess, when I add these two lines
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^ /en? [R=301,L]
redirect working, but mysite.com/en show 404 not found. Someone help me with this?
Your redirect rule as shown in question is working fine. Bot you also need a rewrite rule to handle /en:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
If your browsers ends up at mysite.com/en, your redirection does work, so your question is not how to redirect, but why there's no content at /en on your site. You need to investigate that.

Redirect homepage to a different page using htaccess on a Drupal site

hoping you could give me some help. I'm trying to re-direct:
http://jaffajava.com/oldsite
To
http://jaffajava.com/oldsite/store
What I've tried so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^jaffajava\.com/oldsite\$
RewriteRule (.*) http://www.jaffajava.com/oldsite/store\$1 [R=301,L]
Any help on correcting my syntax/code?
Thanks!
HTTP_HOST variable only matches domain name in the request. You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^jaffajava\.com$ [NC]
RewriteRule ^oldsite(/.*)?$ http://www.jaffajava.com/oldsite/store$1 [R=301,L,NC]
Why redirection? Just set new home page, in Configuration -> System -> Site information

.htacces redirection issue with subfolder php

I want to redirect this link http://www.usedcarstampa.com/blog/?m=201511
to
http://www.usedcarstampa.com/blog/
I am trying the following in .htaccess file
1) Not working
Redirect /usedcarstampa.com/blog/index.php?m=201511 /index.php
2) Not working
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)m=201511(&|$) [NC]
Rewrite-rule ^index.php$ /? [R=301,NC] // not working
note :- main site in core PHP and blog part in word-press
there is also a another ht-access file in blog/ directory. I am also try above code to right there but i not get positive result
please help,thanks in advance
Try this rule as very first rule in your /blog/.htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)m=201511(&|$) [NC]
RewriteRule ^(index\.php)?$ /? [R=301,L]

.htaccess if request url diffrent from some string than

i have a trivial question about htaccess rewrite condition.
The think i want to do in my htaccess is:
When someone accesses my project from a url different from "example.com" to be redirected to a specific controller, for example app_user.php
I need the syntacs for this condition redirect.
Thanks for your time
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/app_user.php
RewriteRule ^ /app_user.php [L]

mod_rewrite Friendly URL clashing with 404

I've been struggling with my mod_rewrite for a while now and I thought I'd cracked it…
My CMS generates blog post URLs with post.php?s= and I've been trying to remove this part of the URL so that they're nice and user friendly. Here's my mod_rewrite:
<ifModule mod_rewrite.c>
DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www.tempertemper\.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php /%1? [R=301,L]
RewriteRule ^resources /archive? [R=301,L]
RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
</ifModule>
Unfortunately, It seems to be directing all pages that aren't found to the blank post.php page instead of the error.php page.
My site is here: http://tempertemper.net and here's a URL that doesn't exist http://tempertemper.net/this-url-does-not-exist
It should take you to http://tempertemper.net/error
Thanks for taking a look :)
Because you are redirecting all paths which fit the [a-zA-Z0-9_-]+ pattern, this will simply send to your post.php script the query string s=this-url-does-not-exist. So it's up to your PHP script post.php to check whether this-is-not-a-url exists or not, as Apache cannot possibly know what blog post ID values are valid and which are not.
As a side note, I'd recommend using /blog/2012-08-24-whatever as the path you give to visitors, rather than /2012-08-24-whatever. That way you can reserve paths for other functions, such as /images without having to write a new exception in your .htaccess file every time you need to create a new utility path.

Categories