htaccess mod-rewrite anchor [duplicate] - .htaccess

This question already has answers here:
'hash' url rewrite in .htaccess
(2 answers)
Closed 8 years ago.
I have this url: mysite.com/account/user#editSucc, I want it to be parsed as mysite.com?goTo=account&section=user&msg=editSucc.
This is my .htaccess file:
RewriteEngine On
RewriteRule ^account/([A-Za-z-]+)$ /?goTo=account&section=$1 [L,NC]
How can I make the part with the # symbol? and merge it with the existing code?
Thanks.

You can't do this using htaccess. The URL fragment (everything after #) is never sent to the server and thus nothing on the server's end even knows it exists. You need a strictly client side solution to deal with it. Since you want it to be redirected (and that's all you'll be able to do with it). you can try something like from this link:
<script type="text/javascript">
var parts = location.href.split('#');
if(parts.length > 1)
{
location.href = '/?goTo=account&section=user&msg=' + parts[1];
}

Related

.htaccess url rewriting not-fix

By using jQuery I have sent a POST value to another file using window.location
where bname refers to some name from database
$.form(window.location + "?rt=" + bname,{project:project,bname:bname},'POST').submit();
after jQuery execution the following url appears on screen: /m/?rt=Jaypee%20Group
However I need the above one as /m/JaypeeGroup
I have tried htaccess rewrite but can't fix this issue
Is there any other way ("window.location + "?rt=" + bname") in jQuery or how can I fix this in htaccess?
Don't need for htaccess, just replace the space in javascript :
$.form(window.location + "?rt=" + bname.replace(" ", ""),{project:project,bname:bname},'POST').submit();
Invoke JQuery as follows.
$.form(window.hostname+"/"+context+"/m/"+project+"/"+bname+"/","POST").submit();
and add htaccess rewrite as per your requirements.
yourhostname/context/m/(.*)/(.*)/ .......

URL Rewrite for IIS 7 IP to Domain name

I have a request from a client that they would like for me to have this happen. When a user on the web goes to http://x.x.x.x have it 301 redirect to their domain name http://www.domain.com . I have messed around with rules in URL Rewrite and I have no problem specifying the conditions to determine the request for the IP address, etc. But when I do the redirect, the site goes into a redirect loop.
My question is how can I do this ? I know this is basically redirecting to the same place, hence the loop, but is there any way to make this happen ? For SEO reasons, they don't want visitors to the site to be able to get it via the public IP.
Thanks,
Chris
//////
Ok All, Thanks to some direction and good ideas from Caner, I was able to figure it out.
Here is the ASP code I used:
<%
if Request.ServerVariables("HTTP_X_HOST") = "" then
Dim rd
rd = "http://www.xxx.com" & Request.ServerVariables("PATH_INFO")
Response.Status="301 Moved Permanently"
Response.AddHeader "Location",rd
Response.end
end if
%>
Thanks to all the suggestions!
Chris
It is a kind of tricky, but instead of configuring this on IIS, I suggest your client to add some code in their header file( header.asp file, site.master etc..);
This code can check the request; and you can reach HttpContext.Current.Request.Url.Host.
Using HttpContext.Current.Request.Url.Host, you can do a redirect.
string currentURL = HttpContext.Current.Request.Url.Host;
if(!currentURL.StartsWith("www."))
{
string newURL = HttpContext.Current.Request.Url.Host;
if(currentURL =="www.x.x.x.x")
newURL ="www.domain.com";
newURL += HttpContext.Current.Request.Url.PathAndQuery;
Response.Redirect(newURL);
}
Hope this helps.

Codeigniter 2.1 and .htaccess - rewrite url

I need to rewrite this url:
domain.com/mali_oglasi/index/1(any number)
to:
domain.com/mali_oglasi
In my .htaccess file I have this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
How can I do this?
If the only thing you want is to map your controller/method differently than the default behaviour, you can use the route.php config file. See the official documentation here : http://codeigniter.com/user_guide/general/routing.html
In your case you'll have something like this :
$route['mali_oglasi/index/(:num)'] = 'mali_oglasi';
Later in your controller you can still get the original digit by using :
$this->uri->rsegment(3);
instead of :
$this->uri->segment(3);
(see official documentation here : http://codeigniter.com/user_guide/libraries/uri.html )
EDIT:
In fact, if you just wish to get rid of the "index" segment when you need to add parameter, you may want to do the inverse of my first answer :
$route['mali_oglasi/(:num)'] = 'mali_oglasi/index/$1';
With that line, every request in the form of "www.yourdomain.com/mali_oglasi/1" will be interpreted by codeigniter as if it were "www.yourdomain.com/mali_oglasi/index/1". Meaning the method "index" of the controller "mali_oglasi" will be used to handle your request.
If you need to retrieve the digit, you want to use :
$this->uri->segment(3);
So if your client should ever go to the url "www.yourdomain.com/mali_oglasi/index/1" directly, you will still retrieve the good uri segment. ( $this->uri->segment(n); give you the n-th segment after route.php rewrite the uri, and $this->uri->rsegment(n) give you the n'th segment before the uri is rewritten. )
I suggest to redirect the user to the new URL :
in your controller mali_oglasi >> in the function index
put the below line
redirect('mali_oglasi');
e.g.
class mali_oglasi extends CI_Controller{
function Index($id){
// Note : make sure you have loaded the url helper
redirect('mali_oglasi');
}
}
Note: don't forget to load the url helper
Note: Set the $config['index_page'] = ''; instead of index in application/config/config.php

Jquery ajax() messing with my .htaccess mod_rewrite

I am performing a simple AJAX() request using Jquery (Google hosted 1.7.1 jquery.min.js code)
The code is pretty simple:
$.ajax({
type: "POST",
url: "../inc/ajax_msgread_sendPM.php",
data: "fromuserid=<?php echo $fromuserid; ?>&pmSubject=<?php echo urlencode($pmSubject); ?>&pmBody=" + pmReply,
success: function(data){
$("#showSuccess").show("fast");
$("#resultResponse").html(data);
}
});
The mod_rewrite .htaccess for this document is:
RewriteRule ^messages/read/([^/]+)/([^/]+)/?$ /msgread.php?usernam=$1&keynode=$2 [QSA,L]
When I view the $resultResponse for some reason the Ajax keeps wanting to turn $1 into 'inc' so any unrelated (or related) mySQL queries using $_GET["usernam"] from the URL ends up returning 'inc'
Why is this happening? There is no relation between my script and the mod_rewrite. There are no variables named "usernam" or "1" on the script (or anywhere on the site).
Firebug gives no help.
Advice please?
UPDATE:
I see where the problem is coming from.. in the ajax jquery code:
url: "../inc/ajax_msgread_sendPM.php",
the "inc" keeps getting set as the username because of its location based on the mod_rewrite rules... I need to somehow exclude this from mod_rewrite... just not sure how to solve this problem
As long as current url for your page is
www.domain.com/messages/read/username/NQ
and you use relative path - it is being rewritten to
www.domain.com/messages/read/username/inc/ajax_msgread_sendPM.php
which is definitely not what you want. The simplest solution would be to change the ajax endpoint url to:
url: "/inc/ajax_msgread_sendPM.php",

HTACCESS - Block everything but specified SEO friendly URL

I haven't found all the answer to my current problem.
Here is the root of the site:
cache
img
display.php
admin.php
What I need is to block all the direct access of the files and allow only access via url formatted like that:
1 ht*p://sub.domain.com/image/param/size/folder/img.jpg (param, size, folder, img are parameters)
2 ht*p://sub.domain.com/action/param1/param2/ (param1, param2 are parameters)
1 would point to display.php with the correct parameters
2 would point to admin.php with the correct parameters
Every other access must be 404 (at best) or 403
my rules are (the htaccess is in ht*p://sub.domain.com/):
RewriteRule ^image/([^/]+)/([0-9]+)/([^/]+)/([^/]+)\.jpg display.php?param=$1&size=$2&folder=$3&img=$4 [L]
RewriteRule ^action/([^/]+)/([^/]+) admin.php?action=$1&param=$2 [L]
Those rules work as I want to but I am stuck on how to block any access that does not come from those URL!
Also (as a bonus) I would like to be able to use the same htaccess on diferrent web address without having to change this file.
Thanks in advance
Have you try moving the image out of the public folder and use php to call the image in?
For the PHP files you can use the switch statement (http://www.php.net/switch).
For the admin.php file you can do something like:
$get_action = $_GET['action'];
switch ($get_action) {
case "edit":
case "view":
case "delete":
case "add":
//Continue loading the page
break;
default:
header('HTTP/1.1 403 Forbidden');
die();
}
Note: I don't know how your code looks or works, but you can have an idea base on the code I added.

Resources