Simple rewrite if a word is present in a url - .htaccess

I have about 100 urls which are like these:
http://example.com/en/contact-us
http://example.com/en/about-us
When the someone clicks on any of them, it should the same page but with a parameter that has been appended
for example :
http://example.com/en/contact-us?language=en
How would i go to solve this? I am tring to write something generic that will do the job for the 100 urls i have.
This is what i have got :
RewriteCond %{REQUEST_URI} /en/ [NC]
RewriteRule ^ %{REQUEST_URI}/?language=en

Internal redirection: URL in the browser doesn't change
RewriteEngine on
RewriteRule ^/?([a-z]+)/([^/]+)/? $1/$2?language=$1 [L]
External redirection: URL in the browser changes to new URL
RewriteEngine on
RewriteRule ^/?([a-z]+)/([^/]+)/? $1/$2?language=$1 [R=301,L]
Please try this

I think you may have forgotten to put a .php file extension at the end?
This will match any two-letter language folder followed by another directory, as long as there is no period.
RewriteRule ^/(\w\w)/([^/]+)/?$ $1/$2.php?language=$1
Be sure to test it with your URLs at regex101.

Related

I want to use a RewriteRule in .htaccess to allow subfolders

We have a url mydomain.com/events that needs to include a country code just before th events folder like this mycomain.com/uk/events. The country could be any number of different codes. I want to use a RewriteRule like:
RewriteRule ^([a-zA-Z0-9-/]+)/events/*.* /events/*.*
so any any url like these will work:
mydomain.com/uk/events
mydomain.com/uk/events/index.php
mydomain.com/uk/events/list-events.php?s=12
With your shown samples/attempts, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Place your htaccess file along with your domain folder(not inside it beside it).
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^/]*)/(?:[^/]*)/([^/]*)(/.*?\.php(?:\?[^=]+=\d+)?)?\s [NC]
RewriteRule ^ %1/%2%3 [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.

Pretty url doesn't show in url-bar

I have an htaccess file with several redirects, Now I want to create a pretty url for some link. I tried the following sentence and it does nothing:
RewriteEngine On
RewriteRule ^/example1.html /?page_id=100 [NC]
When I type www.MyDomain.com/?page_id=100 in my browser, the site shows, but the url still looks the same as I typed it. How would I change my sentence to show example.html in the url-bar instead of ?page_id=100 ?
Thanks in advance
You have a rule that says:
When the browser requests: /example1.html
Then show them: /?page_id=100
It goes one way, it doesn't do anything if the browser requests /?page_id=100. If you want to do something about a browser requesting the query string URL, *you need a rule to tell mod_rewrite to do it*:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=100($|\ |&)&?([^\ ]*)
RewriteRule ^ /example1.html?%3 [L,R=301]
Also, you may want to get rid of the / in the pattern of your rule:
RewriteRule ^example1.html /?page_id=100 [NC]
Leading slashes are stripped off when matching against rules in an htaccess file.

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

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