.htaccess - write a clean URL - .htaccess

I want to write a clean URL for my application. I searched and have written following, but it does not work and gives "Server not found error".
I have URL like this
http://localhost/projectFolder/user_folders/userName/testSite2/
and I want it to be -
http://testSite2.mySystemDomain.com/
I have tried as follows, but it fails.
RewriteEngine on
RewriteRule ^/user_folders/userName/testSite2/(.*) http://testSite2.mySystemDomain.com/$1
EDIT
There will be number of sites like testSite2. I need to write the URL for each such site.

Put this in .htaccess in folder projectFolder
RewriteEngine on
RewriteRule ^user_folders/(.*)/(.*) http://$1.mySystemDomain.com/$2 [R=301,L]

Related

Rewrite url via htaccess on apache server

I have a url which is
www.domain.com/index.php?route=ticketsystem/generatetickets
I want people who type in the url www.domain.com/contact to be redirected to the page index.php?route=ticketsystem/generatetickets however have the url bar still show /contact is that possible via htaccess and if so how? I tried the below
RewriteEngine On
RewriteBase /
RewriteRule contact /index.php?route=ticketsystem/generatetickets [L,QSA]
For your shown attempts, please try following .htaccess rules file. Make sure your index.php and .htaccess files are present in same directory/folder. Also better to use & rather than using / for query string.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^contact/?$ index.php?route=ticketsystem&generatetickets [QSA,NC,L]

Amend .htaccess path rewrite to include subdirectory

I currently have the following .htaccess rewrite rule setup on my site:
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.html?id=$1 [QSA]
The rule works in such a way that if I go to the following URL:
http://example.com/dashboard
It won't try and find the dashboard directory that doesn't exist but instead it will keep the URL as is and redirect the user to the root index page. From there I just use javascript to control what view the user will see depending on what path is appended.
The code works exactly as I want it to but i've now had to move my site into a sub-directory on our server. The URL structure is now this:
http://example.com/mysubdir/dashboard
I tried rewriting my rewrite rule to incorporate the directory but have not been successful so far as i'm no .htaccess expert. I tried something along the likes of:
RewriteEngine On
RewriteRule ^([^/d]+)/mysubdir/?$ index.html?id=$1 [QSA]
Could anyone tell me how I can amend my rewrite rule to work in my sub-directory?
You were close - this should do it:
RewriteEngine On
RewriteRule ^mysubdir/([^/d]+)/?$ /mysubdir/index.html?id=$1 [QSA]
Demo here: http://htaccess.mwl.be?share=6e574cc6-90a4-54ca-b113-ce72d6eb5203

How to rewrite url in htaccess without redirecting on localhost?

Trying to ask my question again and explain it the best I can.
I am running WAMP on windows 10. Apache version 2.4.18. My project is in www folder named hoidja.ee. mod_rewrite is enabled, AllowOverrideis set to All.
Inside that folder I have .htaccess file, where I am trying to accomplish rewrite without redirect and it does not work at all. I know the .htaccess file is working, since I can rewrite index.php to /home/ for example and it is working perfectly fine.
I have a page with all the users listing. You can click on the user, and it will take you to a url:
http://localhost/Hoidja.ee/hoidja.php?user_id=94&username=John
How I would like this to show is
http://localhost/Hoidja.ee/user/John
without any redirection.
What I have tried:
#tried first with only user_id
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule ^hoidja\.php$ /user/%1? [L]
This gave me 404 that /user/94 doesn't exist.
RewriteRule ^user/([0-9]+)/([A-Za-z0-9-]+)/?$ hoidja.php?user_id=$1&username=$2 [QSA]
This doesn't do anything visually. If I enter http://localhost/Hoidja.ee/user/94/John in the url manually, it goes to that page and shows the user data, but if I enter the original url it does not rewrite it. I am using <base> as well.
I have tried all the possible ways but nothing seems to work. How can I accomplish the url rewrite?
You are getting the 404 error because you are rewriting to a non-existent path /user/94 .you will need to redirect /hoidja.php?user_id= to /user/94 and then rewrite /user/94 back to the real location.
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule hoidja\.php$ /hoidja.ee/user/%1? [L,R]
RewriteRule ^(?:hoidja\.ee/)?user/(.+)$ /hoidja.ee/hoidja.php?user_id=$1 [L]

How to write url rewrite in codeigniter

I write the url rewrite in .htaccess in codeigniter as follows
RewriteEngine on
RewriteRule ^Vorarlberg.html$ index.php/users/suchergeb?short_query=true&sbundesland=V&offset=0&limit=25&country=A [L]
RewriteRule ^Tirol.html$ index.php/users/suchergeb?short_query=true&sbundesland=T&offset=0&limit=25&country=A [L]
myproject is located in wamp server under irat.2013 folder
the above code is working but we dont want to show full url, we would like to show
as follows
irat.2013/Vorarlberg.html
and one more thing if we give the full localhost url before the index.php/users... then only it is working other wise it is not.
Please help me ....
Use the application/config/routes.php file. Check docs here: http://ellislab.com/codeigniter/user-guide/general/routing.html

can not sitemap after url rewrite

I have written a rewrite rule in the .htaccess file for my website page:
RewriteRule ^nice_url/$ ?p_action=user_profile&post_author=45 [L]
The full link is: "http://www.example.co.il/nice_url/" it works perfect.
BUT, when I try to create a sitemap (e.g. with http://www.web-site-map.com/), I have an indication that the link "http://www.example.co.il/nice_url/" is broken.
Why?
The link is working fine. Why is it indicated as broken?
Thanks!
You can just add another rule to externally redirect long URL to nice URL like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?p_action=user_profile&post_author=45\s [NC]
RewriteRule ^ nice_url? [R=301,L]
UPDATE: Alright I checked that sitemap generator is putting & instead of & in generated URLs.
You need to add this additional rule to handle this:
# convert & to &
RewriteCond %{QUERY_STRING} ^(.*)&(.*)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1&%2 [L,R,NE]
Seems that I found the problem:
I have a main .htaccess file that redirects to 3 different folders. Each folder is for a different website (all in the same host).
One of the websites is in WP. The rewrite happens well.
BUT, when I try to generate a sitemap - the wp returns a 404 for the sitemap generator and thats why I get an error.
solution: I'll probably try to rewrite from the php and not from the htaccess.

Resources