Mod_rewrite-htaccess: using URL variable as url prefix (sub domain) - .htaccess

I have a website which is currently developed using Drupal.
I want to use a custom URL redirect using htaccess for multilingual website.
At the moment the original url is: http://mydomain.com/page-title/?lang=en
What I want to use is something like this http://en.mydomain.com/page-title
How should I do that?
Thank you.

Try this:
RewriteCond %{HTTP_HOST} ^(en).(mydomain.com)$
RewriteCond %{QUERY_STRING} !lang=
RewriteRule ^(.*)$ /$1?lang=%1 [L,QSA]
So when access http://en.mydomain.com/page-title, it will rewrite to /page-title?lang=en

Related

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

htacces rewrite url - redirect url to sub folder without www - oc

I want to redirect url to a sub folder without www. like,
If anyone type
http://WWW.mywebsite.com/theme1/
Then, I want this result,
http://mywebsite.com/theme1/ (without www)
theme1 is a folder.
Thanks in advance.
Try it like this,
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R,QSA,L]
Dear This can be done via masking, you can use https://bitly.com/ to shorten your website url or you can use service provider such as GoDady, to get the same

How can I redirect hits to a querystring URL via HTACCESS

I've created a website with a decent WordPress theme and framework about events but there's some links to a pretty annoying page like this:
/?event-category=gigs
I would like to redirect any links to URLs like that simply to:
/gigs
I'm hopeless in Htaccess, can someone suggest the syntax please? I'd appreciate it.
You can use this rule just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} (^|&)event-category=(gigs) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

Rewrite http://example.com/test.asp?index=3 to http://example.com/about

I need to Rewrite the old urls generated by ISS to a new system we have build (Joomla).
The url's had to be google friendly. What we want to happen:
Rewrite http://example.com/test.asp?index=3 to http://example.com/about
I've used a few Rewrite's i knew, but they dont work:
RewriteRule ^/test.asp?index=3 / [R=301,L]
RewriteRule ^/test.asp?index(.*)3 / [R=301,L
What pice of code am i missing/doeing wrong?
Kind regards.
You must use QUERY_STRING to check query string.
You can use this code in your htaccess (in document root folder)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index=3$ [NC]
RewriteRule ^test\.asp$ /about [R=301,L]
Note: put this rule before Joomla's rules

Re-route request to domain without subdomain

I currently have my blog at blog.flohei.de. I'd like to drop the blog and run everything at flohei.de directly. Old links to blog.flohei.de/foo/bar.html should then automatically redirected or mapped to flohei.de/foo/bar.html. I think this is possible using a .htaccess file, right? But how do I exactly do this? Where do I put that file? Thanks!
Put this code in your DOCUMENT_ROOT/.htaccess file of blog.flohei.de:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.(flohei\.de)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]

Resources