0

I have a URL link like, http://domain.com/abs/def/city and,

i want to display it as http://city.domain.com/ABC/def

using .htaccess.

Can any one help me by providing .ht access rules.

I want to write .htaccess rules for each city name in URL act as sub domain name. Also i want it to be dynamic as there are different cities are available in site.

i am using below code in .htaccess file, but not working properly.

RewriteRule ^index.php/(.)/(.)/([^/]+)$ http://$3.domain/$1/$2/$3 [R=301,L]

is there any way to get my requirement using or by modifying my above code or by some other .htaccess code.

1 Answer 1

0

Sorry, but what you ask is not possible. This is a typical missunderstanding about url rewriting:

Url rewriting rewrites (manipulates) incoming requests on the server side before processing them. It is not possible to alter outgoing content such that contained urls are changed by this means.

There are solutions for that though:

  • apaches proxy module can "map" one url into the scope of some other url
  • there are also modules for automatic post processing of generated html markup
  • more exotic or creative solutions exist, it depends on your situation in the end...

But usually the easiest is to change the application (typically just its central configuration) such that it contains final urls (pointing to the subdomain in your case). Then you can indeed use the rewriting module to "re-map" those to the previous scope when future incoming requests refer to them (they got clicked).


Ok, second step getting additional info from your comments:

Just to get this clear: you understand that it is not possible to change the link you send out by means of rewriting, but you want to change the url shown in the browser after the user has clicked on some city link? That is something different to what you wrote before, that actually is possible. Great.

If the rewriting works as you want it to (you see the desired url in the browsers address bar), then we can go on. The error message indicates a name resolution problem, that has nothing to do with rewriting. Most likely the domain "cambridge.192.168.2.107" cannot be resolved, which is actually not surprising. You cannot mix ip addresses and names, it is either or.

Also I see that you are using internal, non-routable addresses. So you also are responsible for the name resolution yourself, since no public DNS server can guess what you are setting up internally. Did you do that?

I suggest these steps:

  1. stop using an ip addres for this, use a domain name.
  2. since you are working internally, take care that that domain name is actually resolved to your local systems ip address. How you do this depends on your setup and system, obviously. Most likely you need some entry in the file /etc/hosts or similar.
  3. you need to take care that also those "subdomain names" get resolved to the same address. This is not trivial, again it depends on the setting and system you locally use.
  4. if that name resolution works, then you should see a request in your http servers access log file. Then and only then it makes sense to go on...
Sign up to request clarification or add additional context in comments.

8 Comments

is there any .htaccess code that can be help to get my goal.like i am using below .htaccess code but not working properly as it redirect as "page not found!" RewriteRule ^index.php/(.)/(.)/([^/]+)$ http://$3.domain/$1/$2/$3 [R=301,L]
Sorry, I cannot answer that since I have no details about the situation. What url is requested and what is it rewritten to? you know that, not me. A hint: as ALWAYS when trying to setup or debug rewriting rules: ENABLE REWRITE LOGGING! Check the excellent documentation of the rewriting module, you will find the commands RewriteLog and RewriteLogLevel in there. They help you to understand exactly what is actually going on inside the rewriting engine for each request. Don't try to guess what happens, switch on the lights (logging) and take a close look!
ok, see i have site having product listing by city name.so there is list of city and each city name having link like domain/state/city. on that page there is a list of product having this city. but i want to re-write my url as : city.domain/state. i use this link as reference:stackoverflow.com/questions/11558384/… so instead of URL domain/state/city , i want to be city.domain/state
I did understand that. But as written: this is not possible by means of rewriting. The rewriting module simply cannot alter the links you send out. If you send html markup containing like like domain.com/state/city then these links go out like that. The rewriting module cannot be used to alter them. It can only alter incoming requests.
i fallow the below link code which match my requiremnt but not working properly. stackoverflow.com/questions/11558384/… can you help me by find out is there any mistack to use that code in my site.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.