How to remove part of url with .htaccess?

I currently have 20+ URLs on my site in this formate

http://www.example.net/content/index/mission

I want to remove / content / index from all urls, so they should look something like this.

http://www.example.net/mission

In other words, I would always remove / content / index from url. I'm sure this is really straight forward, but I'm not very experienced with Apache.

+7
source share
5 answers

You will need the Apache rewrite module: mod_rewrite .

Then do something like this:

 RewriteEngine on RewriteRule ^content/index/(.*)$ $1 

Here is the official documentation for mod_rewrite: click

+12
source

I'm going to guess that you already have rewriting rules for URLs like http://www.example.net/content/index/mission

You need to find these rules and add a new one that uses a similar structure, but the β€œhard codes” of the parts of the content / index, for example, suppose that the existing one was

 RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1&param2=$2 [L,qsa] 

You want to create a new rule to pick up / complete the task and rewrite it in a similar way, but before the existing rule is executed, for example

 RewriteRule ^mission$ /content.php?param1=index&param2=mission [L,qsa] RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1&param2=$2 [L,qsa] 

These are just examples - it will really depend on your existing rules.

+1
source
  # Get rid of index.php RewriteCond %{REQUEST_URI} /index\.php RewriteRule (.*) index.php?rewrite=2 [L,QSA] # Rewrite all directory-looking urls RewriteCond %{REQUEST_URI} /$ RewriteRule (.*) index.php?rewrite=1 [L,QSA] 

Or just add index.html depending on which extension you want to remove, and for Apache, MOD_REWRITE is required for Apache. Thanks.

0
source

I also have a script. I wrote what you put on your server, then go to it through an Internet browser and it can confirm if you have mod_rewrite on your server. The way I showed you works 100% while mod_rewrite is on.

0
source
 RewriteEngine On RewriteCond %{REQUEST_URI} ^/removedstring/ RewriteRule ^removedstring/(.*)$ https://www.domain.eu/$1 [L,NC,R=301] 
0
source

Source: https://habr.com/ru/post/898954/


All Articles