url-rewriting interview questions
Top url-rewriting frequently asked interview questions
Using this RewriteRule in my .htaccess file I'm getting RewriteRule: Bad flag delimiters which is returning a 500 error in the browser. Can anyone point me in the right direction please. Thanks.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^dev/(.*)$ http://dev.example.com/$1 [L,R=301, NC]
This is on Ubuntu on a Digital Ocean Droplet.
Source: (StackOverflow)
I have a website with stories in it. I can have multiple types of stories within multiple categories like:
- children
- romance
- scifi
- action
- thriler
- quests
The stories are accessible using urls like:
www.example.com/action/story-name-action/
www.example.com/romance/story-name-romance/
and the first param (action) and the second (story-name-action) are redirected with .htaccess using rules. This part works just fine.
Lately, I get few dozen of 404 from different sites and here's what I want to do but I dont know how:
If someone types, for example: /action/story-nme-ction
, I want to redirect to: action/story-name-action/
Is there an efficient way to implement this?
Source: (StackOverflow)
What I want is to have links which change a part of the page , and a dynamic URL for it, where I can specify variables such like #calendar=10_2010tabview=tab2
Check this for an exact example:
CLICK HERE FOR EXACT DEMO
So here is the link format what I need:
#calendar=10_2010&tabview=tab2
I need to have variables in the links like calendar
and tabview
so I can change multiple things on a single page without realoading.
Or another format such like on http://www.wbhomes.com.au , this is exactly what I want, however the first format is good too but this is much more beautiful.
http://wbhomes.com.au/#/propertiesforsale/houseandland/quinnsbeach-waterland1
Requirements
Needs to be accessed from anywhere
from example a mail, or if I just
write in the url bar.
The link should be in the history, so if If I push the back or forward button the page needs to be accessed.
Page refresh needs to work too.
Some recources:
Examples:
Some Tutorials:
Please help me! I've never found any solution to do this, but I don't want to use jquery or any API, or any library, I want to have a working Javascript/AJAX
and PHP
script.
Source: (StackOverflow)
I don't understand anything about IIS, but am trying to solve this problem of redirecting all visitors to domain.com/page to domain.com/page.html
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="/page" value="/page.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
A couple of problems arise:
- I don't know where to even put the file. There is a User root directory, and an htdocs directory, I tried both, no joy.
- I don't even know if the account can do rewrites, I am trying to find that out.
Source: (StackOverflow)
I upgraded my site to use ASP.Net MVC from traditional ASP.Net webforms. I'm using the MVC routing to redirect requests for old .aspx pages to their new Controller/Action equivalent:
routes.MapRoute(
"OldPage",
"oldpage.aspx",
new { controller = "NewController", action = "NewAction", id = "" }
);
This is working great for pages because they map directly to a controller and action. However, my problem is requests for images - I'm not sure how to redirect those incoming requests.
I need to redirect incoming requests for http://www.domain.com/graphics/image.png to http://www.domain.com/content/images/image.png.
What is the correct syntax when using the .MapRoute()
method?
Source: (StackOverflow)
When building code like this:
<script type="text/javascript" src="<%=ResolveUrl("~/js/js.js")%>"></script>
or
<input type="image" src="<%=ResolveUrl("~/img/submit.png")%>" />
Should I use Url.Content
or ResolveUrl()
? What's the difference?
Source: (StackOverflow)
Best way managing session in Java. I heard that cookies are not reliable option for this as they gets stored into browser and can be accessed later on? Is this correct? If possible please come up with the answers with the coding example.
Which is the best among:
- URL Rewriting: Server will add an additional parameter at the end of URL link
- Hidden parameter in Form: server will add an additional parameter at every form in HTML
- cookie: Server will ask browser to maintain a cookie.
Source: (StackOverflow)
I've got some RESTful services running in a pure WCF context (i.e. ASP.NET compatibility is not enabled, and thus there is no HttpContext.Current
object available).
The URLs to the services are rewritten at the start of the request using an IHttpModule
(which at that point does have an HttpContext
and rewrites it using HttpContext.Current.RewritePath
) to get rid of things like the .svc
extension from the URL.
However, I need to access the original URL that was requested from within the WCF infrastructure. Is there an equivalent to HttpContext.Current.Request.RawUrl
on the OperationContext
or WebOperationContext
classes anywhere? Using WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri
returns the rewritten URL not the original one.
Source: (StackOverflow)
I see the Nginx HttpRewriteModule documentation has an example to rewrite a www-prefixed domain to a non-www-prefixed domain:
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}
How can I do the reverse-- rewrite a non-www-prefixed domain to a www-prefixed domain? I thought maybe I could do something like the following but Nginx doesn't like the nested if statement.
if ($host !~* ^www\.) { # check if host doesn't start with www.
if ($host ~* ([a-z0-9]+\.[a-z0-9]+)) { # check host is of the form xxx.xxx (i.e. no subdomain)
set $host_with_www www.$1;
rewrite ^(.*)$ http://$host_with_www$1 permanent;
}
}
Also I wanted this to work for any domain name without explicitly telling Nginx to rewrite domain1.com -> www.domain1.com, domain2.com -> www.domain2.com, etc. since I have a large number of domains to rewrite.
Source: (StackOverflow)
I have the AngularJS seed project and I've added
$locationProvider.html5Mode(true).hashPrefix('!');
to the app.js file. I want to configure IIS 7 to route all requests to
http://localhost/app/index.html
so that this works for me. How do I do this?
Update:
I've just discovered, downloaded and installed the IIS URL Rewrite module, hoping this will make it easy and obvious to achieve my goal.
Update 2:
I guess this sums up what I'm trying to achieve (taken from the AngularJS Developer documentation):
Using this mode requires URL rewriting on server side,
basically you have to rewrite all your links to entry point of your
application (e.g. index.html)
Update 3:
I'm still working on this and I realise I need to NOT redirect (have rules that rewrite) certain URLs such as
http://localhost/app/lib/angular/angular.js
http://localhost/app/partials/partial1.html
so anything that is in the css, js, lib or partials directories isn't redirected. Everything else will need to be redirected to app/index.html
Anyone know how to achieve this easily without having to add a rule for every single file?
Update 4:
I have 2 inbound rules defined in the IIS URL Rewrite module. The first rule is:
The second rule is:
Now when I navigate to localhost/app/view1 it loads the page, however the supporting files (the ones in the css, js, lib and partials directories) are also being rewritten to the app/index.html page - so everything is coming back as the index.html page no matter what URL is used. I guess this means my first rule, that is supposed to prevent these URLs from being processed by the second rule, isn't working.. any ideas? ...anyone? ...I feel so alone... :-(
Source: (StackOverflow)
About the system
I have URLs of this format in my project:-
http://project_name/browse_by_exam/type/tutor_search/keyword/class/new_search/1/search_exam/0/search_subject/0
Where keyword/class pair means search with "class" keyword.
I have a common index.php file which executes for every module in the project. There is only a rewrite rule to remove the index.php from URL:-
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
I am using urlencode() while preparing the search URL and urldecode() while reading the search URL.
Problem
Only the forward slash character is breaking URLs causing 404 page not found error.
For example, if I search one/two
the URL is
http://project_name/browse_by_exam/type/tutor_search/keyword/one%2Ftwo/new_search/1/search_exam/0/search_subject/0/page_sort/
How do I fix this? I need to keep index.php hidden in the URL. Otherwise, if that was not needed, there would have been no problem with forward slash and I could have used this URL:-
http://project_name/index.php?browse_by_exam/type/tutor_search/keyword/one
%2Ftwo/new_search/1/search_exam/0/search_subject/0
Thanks,
Sandeepan
Source: (StackOverflow)
I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.
Directories to exclude: "admin" and "user".
So http requests: http://www.domain.com/admin/ should not be passed to index.php file.
ErrorDocument 404 /index.php?mod=error404
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
Source: (StackOverflow)
I was planning to use url routing for a Web Forms application. But, after reading some posts, I am not sure if it is an easy approach.
Is it better to use the URL Rewrite module for web forms? But, it is only for IIS7. Initially, there was some buzz that URL routing is totally decoupled from Asp.Net MVC and it could be used for web forms.
Would love to hear any suggestions..
Source: (StackOverflow)