EzDevInfo.com

url-rewriting interview questions

Top url-rewriting frequently asked interview questions

How to remove index.php from URLs?

All of my URLs on my Magento installation require index.php in them, like:

http://store.com/index.php/admin/

http://store.com/index.php/customer/account/login/

The problem is that the system by default links to URLs like

http://store.com/admin/

http://store.com/customer/account/login/

Which look prettier anyway. I assume this is a rewrite issue in .htaccess, but as tinkering with that in the past has given me 500s, I'd like to ask you guys first.

Changing the SEO settings, flushing the configuration cache, and reindexing URLs did not work as suggested here.


Source: (StackOverflow)

RewriteRule Error: Bad flag delimiters

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)

Advertisements

redirect 404 to similar urls

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)

How to show Ajax requests in URL?

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)

IIS URL Rewrite and Web.config

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:

  1. 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.
  2. I don't even know if the account can do rewrites, I am trying to find that out.

Source: (StackOverflow)

How do I route images using ASP.Net MVC routing?

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)

Should I use Url.Content() or ResolveUrl() in my MVC views?

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)

ASP.NET URL Rewriting

How do I rewrite URL's in ASP.NET?

I would like users to be able to goto http://www.website.com/users/smith instead of http://www.website.com/?user=smith


Source: (StackOverflow)

Best option for Session management in Java

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)

What is the WCF equivalent of HttpContext.Current.Request.RawUrl?

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)

Nginx rewrite non-www-prefixed domain to www-prefixed domain

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)

How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?

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:

IIS URL Rewrite Inbound Rule 1

The second rule is:

IIS URL Rewrite Inbound Rule 2

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)

urlencoded Forward slash is breaking URL

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)

.htaccess mod_rewrite - how to exclude directory from rewrite rule

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)

IIS URL Rewriting vs URL Routing

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)