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

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]

Related

redirect url from htaccess file

I'm trying to bulk redirect my site link like this,I need to remove home from every link and redirect it to root directory as shown below.
example.com/home/hello-world.
example.com/home/tag/world.
to
example.com/hello-world
example.com/tag/world.
I'm using these code
RewriteEngine On
RewriteCond %{REQUEST_URI} (.+)/home(.*)$
RewriteRule ^ %1/ [R=301,L]
Considering that your htaccess rules file have more rules apart from your shown ones, which will take care of handling pages from backend(rewrite), if this is the case then please try following htaccess rules file.
Please these rules at top of your htaccess rules file. Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /example.com/
RewriteRule ^home/(.*)$ /$1 [R=301,L]

htaccess replace the parameter in the url

I have a URL like this :
https://example.com/coins/1&order_by=today and several categories such as:
https://example.com/coins/1&order_by=new
https://example.com/coins/1&order_by=trending
You can also change the page so it will be 2 instead of 1 etc.. like : https://example.com/coins/2&order_by=today
I would like to change these to have https://example.com/today?page=1 etc instead of this above
I have read many responses on this subject and I searched the web, but unfortunately no solution works.
I tried this but it does not works:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*&)?order_by=new(&.*)?$
RewriteRule ^(.*)$ $1?new/%1/%2 [L]
Try following htaccess Rules, please make sure to place these Rules at top of your htaccess file. Make sure to place your htaccess file inside root folder along with coins folder(not inside coins folder).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/coins/(\d+)&order_by=(\S+)\s [NC]
RewriteRule ^ /%2?page=%1 [R=301,L]

Simple rewrite if a word is present in a url

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.

Got stuck in a very basic .htaccess rewrite

<h1>' . $name. '</h1>
The above is the reference which points to my profile1.php file. This file is called index.php . It currently displays the urls as this:
http://www.domain.com/interact/profile1.php?id=36
I have tried implementing the .htaccess file to rewrite the url. I tried many combinations and most of them gave a 500 error and some did not rewrite the url.
This is the .htaccess file which I use, it does not make the url to change.
I want the url to look like http://www.domain.com/interact/profile/36
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile1.php/$1 [QSA,L]
</IfModule>
I know this is a very basic question but I seem to stuck in it and have read basic tutorials but am not able to implement it properly.
The files index.php ,profile1.php and .htaccess are in folder named interact.
Tell me any changes required in php or .htaccess files.
It currently displays the urls as this: http://www.domain.com/interact/profile1.php?id=36
...
I want the url to look like http://www.domain.com/interact/profile/36
Step 1:
Change your content to have links like this:
<h1>' . $name. '</h1>
This way, when you click on a link the URL that will appear in the URL address bar is will look like: http://www.domain.com/interact/profile/36
Step 2:
Then you need to use these rules to internally change it back:
RewriteEngine On
RewriteBase /interact/
RewriteRule ^profile/([0-9]*) profile1.php?id=$1 [L,QSA]
In order to point any external links, like google index bots to the new URLs, you'll need to add these as well:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /interact/profile1\.php\?id=([0-9]*)
RewriteRule ^ http://www.domain.com/interact/profile/%2 [L,R=301]
try this
RewriteRule ^interact/profile1.php/(.*)$ interact/profile1.php?id=$1 [L]
i don't know what make QSA modifiers does so i remove it. If you know what, use it)

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