URL Rewriting for url with countrycode - .htaccess

I am writing a multi language website. Therefore I would like some help with a URL rewrite problem.
Case:
When someone visits www.example.com without adding a country code (nl, en, de) the htaccess redirects the visitor to www.example.com/nl/ i.g.
RewriteRule !(nl|en|de)(.*).* /nl/ [R=301,L]
The website is renewed and has got many url's directing to the website (google, forums). i.g. www.example.com/oldpage-nomore.html. What I would like is the following; the htaccess should detect that the request uri doesn't contain nl,en or de and should redirect to pagenotfound.php. RewriteRule ^(.*)\.html /public/oldurl?section=nl&notfound=$1$2&basehref=true&%1 [PT,L] the problem with this Rewrite rule is: all files ending in .html are being redirected.
What I am looking for is the following:
When someone visits www.example.com and no request uri is entered this should redirect to www.example.com/nl/
When there is a requested uri and this doens't contain a countrycode (nl|en|de) than redirect to pagenotfound.php
I tried the following but it doens't work:
RewriteCond %{REQUEST_URI} !^(nl|en|de)$
RewriteRule ^([a-z]{2})/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
I hope someone can help.
Thank you in advance.

Try to place this .htaccess file at the server root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^.*$ /nl/ [R]
RewriteCond %{REQUEST_URI} !^/(nl|en|de)
RewriteRule ^.*/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
(you probably made several mistakes, I tried to fix them)
I'm not sure with the first RewriteCond %{REQUEST_URI} regular expression - maybe remove the question mark or slash, I don't now... can't test now.

Related

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

Part of my .htaccess code is as follows:
RewriteRule ^([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
This basically redirects URL, example.com/XX to example.com/nextlevels_state.php?state=XX and example.com/XX/1 to example.com/nextlevels_state.php?state=XX&page=1 (internally).
Now, I would like to change the URL structure of the URL from example.com/XX to example.com/nextlevels/XX and example.com/XX/1 to example.com/nextlevels/XX/1
and I tried changing .htaccess to as follows:
RewriteRule ^nextlevels/([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
However, as the site urls are already indexed in search engines, I would like to know a way to redirect all the traffic from example.com/XX to example.com/nextlevels/XX (externally) using .htaccess .
Please guide me in this regard. Thank you community :)
Could you please try following, written and tested with shown samples only(improving your already done attempts here). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for redirect to url example.com/nextlevels/XX.
RewriteRule ^([A-Z]+)$ nextlevels/$1 [R=301]
RewriteRule ^nextlevels/([A-Z]+)/?$ nextlevels_state.php?state=$1 [NC,L]
##Rule for redirect to url example.com/nextlevels/XX/1.
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels/$1/$2 [R=301]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC,L]

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.

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

htaccess rule with exception doesn't work properly

I want to redirect all urls under a domain, to another domain, except one or 2 urls.
For example, all urls under domain1.com redirect to domain2.com except the url "domain1.com/cat01"
I wrote this:
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteCond %{REQUEST_URI} !^/cat01$
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
It works in all urls but it redirects "domain1.com/cat01" to "domain2.com/index.php"!
I tried other ways (for example excepting by regex in rewriterule ... but the problem was exsist!
(I use joomla htaccess codes there too)
Thanks Joe
I found another way. I installed ReDJ component on my Joomla,and the problem solved.
but also that way won't work I think:
RewriteCond %{REQUEST_URI} !^/?cat01$
Because I've tried that with and without "/" at start.

URL Rewrite Conditions & Parameters

----EDIT---
I have just realized that my explanation of the problem was missing an important piece of information.
The URL's should only be redirected if second parameter is present.
So the rule should read:
Redirect any URL that has /d/ in it, ONLY if /d2/ is also found in the URL.
----End Edit__
I have the need to 301 redirect all URL's on a site that contain a specific parameter to the same URL, but with an additional directory included. All of the URL's that require redirection contain a certain directory: /d/ Example:
http://www.mysite.com/category1/d/subcategory1/subdirectory2/
--Should Redirect to --
http://www.mysite.com/newdirectory/category1/d/subcategory1/subdirectory2/
The one thing in common with any of the URLs' requiring redirection is that they all contain a directory /d/ in the URL, which always immediately follows the "category" directory as indicated in bold in sample URL's above. I would then like to insert an additional directory in front of the category directory as indicated in bold in the sample URL's above. The rest of the URL will remain the same.
Can anyone help with this? I'm relatively new to mod_rewrite and realize I can make a big mess if I don't get it right.
Thanks in advance for anyone who can offer hlep
:)
Try this (edited to reflect change in question):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*/d/.* [NC]
RewriteCond %{REQUEST_URI} ^.*/d2/.* [NC]
RewriteCond %{REQUEST_URI} !/newdirectory/ [NC]
RewriteRule ^(.*)/d/(.*)$ http://www.mysite.com/newdirectory/$1/d/$2 [R=301,L]
Try adding the following to your htaccess file in the root directory of your site.
RewriteEngine On
RewriteBase /
#if the url contains a /d2/ [NC]
RewriteCond %{REQUEST_URI} /d2/ [NC]
RewriteRule ^([-a-zA-Z0-9]+/d/[-a-zA-Z0-9]+/[-a-zA-Z0-9]+/)$ /newdirectory/$1 [L,R,NC]

Resources