user1746291
user1746291

Reputation: 333

Overwrite the url in .htaccess file for django

I want to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:

www.abc.com/login/?lang=es

I would like this to be displayed in the address bar as:

www.abc.com/login/es

So please tell me how i can overwrite the existing url in my .htaccess file.

Thanks

EDIT

My directory listing

`

myproject

------------- >my application

------------- >settings/url directory

media
templates`

Upvotes: 1

Views: 524

Answers (1)

Jon Lin
Jon Lin

Reputation: 143846

The first thing you need to do is go through all of your code and change your login links to look like:

www.abc.com/login/es

It'll save you an extra connection and redirect.

Then you need rules in your htaccess file in the www.abc.com document root to redirect the client when the request is made for /login/?lang=something to the nicer looking URL:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /login/?\?lang=([^&\ ]+)
RewriteRule ^ /login/%2? [L,R=301]

Then you need rules to internally rewrite the nicer looking URL back to the one that has the query string:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?login/(.*)$ /login/?lang=$1 [L]

If the base URI has changed (from say, /login to /login/es), you may need to include a base URI in your login page so that relative links will still resolve correctly. You can do this by adding this tag in your page's header:

<base href="/">

Upvotes: 2

Related Questions