htaccess – Apache Rewrite Rule www to non-www + http to https + add trailing slash

I really need the following 3 features of my website:

  • www to non-www
  • http to https
  • force trailing slash at the end of the URLs

I have a current implementation but I face a very bad bug.

When I write manually in the browser “” the browser redirects to “” This, of course, opens my homepage and not the required “/page/”

I tried many examples and read a lot of similar questions for the last few weeks. No success.

Here is my full htaccess file:

RewriteEngine on
RewriteBase /

# Redirects www to non-www
RewriteCond %{HTTP_HOST} ^www.(.*)$ (NC)
RewriteRule ^(.*)$ https://%1/$1 (R=301,L)

# Force ending url traling slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ (L,R=301)

# Prevent CI index.php

RewriteCond $1 !^(index.php|resources|robots.txt)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .* index.php/$0 (PT,L) 
RewriteRule ^(.*)$ index.php?$0 (L)

# Prevent user access to the CI system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 (L) 

# Prevent user access to the CI application folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 (L)

# Redirects http to https protocol
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

# Redirect index.php to the root /
RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ /$1 (R=301,L)

# One month for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2628000, public"

RedirectPermanent /free-project-management-certification/
RedirectPermanent /free-scrum-master-certification/
RedirectPermanent /product-owner-certification/
RedirectPermanent /human-resources-management-certification/
RedirectPermanent /product-management-certification/
RedirectPermanent /project-management-certification/

RedirectPermanent /index.php/

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0(678) no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent

I changed positions of all redirects, tried different combinations, changed characters, a lot of things. I would really appreciate some help.

Nginx proxy pass with rewrite rule

I tried to copying nginx configuration for my site to redirect crawler such as googlebot or facebook. I tried this code but it keeps returning 404

if ($prerender = 1) {
   rewrite .* /$scheme://$host$request_uri? break;

I want to ask about the rewrite rule on that code. if the URL accessed is

would the proxy pass URL became like this?

website – .htaccess rewrite giving 500 error

Hello i used htaccess to rewrite query string url i am getting a 500 method possible, please i need help
Below is the full .htaccess code that i used

RewriteEngine on
RewriteCond %{THE_REQUEST} /((^.)+).php (NC)
RewriteRule ^ /%1 (NC,L)

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php (NC,L)

RewriteCond %{THE_REQUEST} /signin?id=((^s)+) (NC)
RewriteRule ^.+$ /signin/id/%1? (NC,L,R=301)

RewriteRule ^/signin/id/(.*)$ /signin?id=$1 (L)

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} (L,R=301)

DirectoryIndex home.php index.htm index.html index.php

Nginx: Rewrite links on website to different domain

Background: We have a Gitlab instance running, which is reachable over 2 different domains from 2 different networks (that is mandated by the network structure and cannot be changed). Gitlab supports only 1 domain. In the past it worked out of the box, but with the last update the links to the files in a repository are set to the main domain, even when coming from the second domain. So we are able to browse the Gitlab UI through the second domain flawlessly. But as we click on a file in a repository, the domain changes from the second to the main domain (which isn’t reachable from the second network), because the links for the files are absolut URLs with the main domain. This means, that we cannot open a file in the Gitlab Web UI from the second domain. The problem is only, that these are absolute links with the wrong domain. With changing the domain by hand we can still display the files.

The Gitlab Support stated, that it is not supported by Gitlab itself, but it should be possible to configure Nginx (which is used by Gitlab) to rewrite the URLs depending on the requesting domain. I couldn’t find corresponding results, when search on the web (only about rewriting requested URLs, not the links on the site).

To further describe the structure of the problem, please look at this diagram:

Structure of the Gitlab deployment in the networks

The single Gitlab server is reachable over 2 individual loadbalancers (which act here as routers, since they only point to that one server). They are reachable under their own domain ( and respectively) and only through their own network. The Gitlab instance is configured for the domain and cannot directly be configured for a second domain. The problem described above arises when trying to open a file in the web UI while being connected through the second network and All that is embedded into the organizational networks and is not reachable from the internet.

TL;DR: Is it possible to let Nginx rewrite absolute URLs in links on a served website depending on the requesting URL of the website? If yes, how can it be done?

rewrite – Exception in Nginx sub_filter

I want to use the Nginx sub_filter module and I want to except some files to substitute. Is there a good and clean solution for that?
For example, I want to replace str_1 to str_2 in all javascripts, but not those that have some property like location, name, or even size.


301 redirect rewrite query string in .htaccess

RewriteRule ^/contact/?view=full$ /contact/ (R=301,L)

The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. So, the above directive won’t match the required URL. To match the query string you need to use an additional condition and check against the QUERY_STRING server variable.

Also, in .htaccess (directory context) the URL-path matched by the RewriteRule pattern does not start with a slash, because the directory-prefix that is first removed from the URL that is matched by the pattern ends with a slash. So, again, the directive will not match the requested URL because of the slash prefix.

Also, the query string from the request is copied through to the substitution string by default – unless you explicitly discard or create a new query string. So, the above directive won’t remove the query string either.

Try the following instead, near the top of your .htaccess file:

RewriteCond %{QUERY_STRING} ^view=full$
RewriteRule ^(contact/)$ /$1 (QSD,R=302,L)

This matches the URL /contact/?view=full exactly. And redirects to /contact/. The QSD (Query String Discard) flag (Apache 2.4) on the RewriteRule directive discards the query string from the substitution string.

The $1 is a backreference to the captured group in the RewriteRule pattern, ie. the value contact/. This simply saves typing/repetition.

Note that this is currently a 302 (temporary) redirect. If this is intended to be permanent then only change it to a 301 once you have tested that it works OK. 301s are cached persistently by the browser, so can make testing problematic.

You should clear your browser cache before testing.


For Magento2 how to apply a redirect rewrite in .htaccess file for all URL’s from .html to /

For Magento2 how to apply a rewrite in .htaccess for all URL’s from .html to / URL structure. The M1 store has .html as the end, the new M2 store has / as the URL ending for all products and categories.

We have tried the below:

## Redirect .html to /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^((^/)+)/$ $1.html

RewriteCond %{THE_REQUEST} ^(A-Z)+ /((^/)+/)*(^.)+.html(?(^ )*)? HTTP/
RewriteRule ^(((^/)+/)*(^.)+).html$ https://%{HTTP_HOST}/$1 (R=301,L)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.(a-zA-Z0-9){1,5}|/)$ 
RewriteRule (.*)$ https://%{HTTP_HOST}/$1/ (R=301,L)

But it breaks the checkout and some other parts of the site. Any ideas on how to correct please? Looks to be https error on checkout I think.

How to make[post_typeA]/[post_nameB]/[post_nameA] not 404: custom post types rewrite rules. ([PostTypeA]/[ParentPage]/[ChildPage])

I scoured this forum and a lot of others but I have not found the answer and starting to doubt whether it’s even possible..

I’m trying to make a site with week and day posts, a day post is called “palaver”. When you publish a week post, 7 palaver posts are created which are children of the week post. WP automatically makes a URL which is quite pretty: For clarity:

  • palavers is the rewrite of the slug for post_type “palaver”
  • week-1 is the name of the parent of week-1-thursday of post_type “week”
  • week-1-thursday is the name of the post I want to display of post_type “palaver” which is the child of week-1

So both the below are true:

  • (post_type=palaver)/(name_of_post_type=week)/(name_of_post_type=palaver)
  • (post_type=palaver)/(parent-page)/(child-page)

When I turn of pretty permalinks WP generates the following URL: which suggests that the title of the post is week-1/week-1-thursday, however before publishing (when editing the post the URL is the correct post title is displayed and only after publishing does the permalink change to the version WP cannot seem to find any posts for. I don’t know what type of URL would lead to the desired post either (or how to discover that).

Both post types week and palaver are registered similarly.

register_post_type('palaver', array(
    'supports' => array(
    'rewrite' => array('slug' => 'Palavers'),
    'has_archive' => true,
    'public'=> true,
    'hierarchical' => true,
    'taxonomies' => array('gebied'),
    'menu_icon' => 'dashicons-calendar-alt',
    'labels'=> array(
        'name' => 'Palavers',
        'add_new_item' => 'Add new Palaver',
        'edit_item' => 'Edit Palaver',
        'all_items' => 'All Palavers',
        'Singular name' => 'Palaver'

For the programmatic generation of the palaver posts I use the following code:

function create_days_week($new_status, $old_status, $post){
if ('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'week'){
    $week = array(
    "0" => 'Zaterdag',
    "1" => 'Zondag',
    "2" => 'Maandag',
    "3" => 'Dinsdag',
    "4" => 'Woensdag',
    "5" => 'Donderdag',
    "6" => 'Vrijdag'

    $latestposts = new wp_query(array(
        'post_type' => 'week',
        'orderby'   => 'post_date',
        'order'     => 'desc',
    $latestpost = $latestposts->posts;
    $CategoryName = get_the_terms($latestpost(0)->ID, 'gebied');
    $CatTermID = array($CategoryName(0)->term_taxonomy_id, $CategoryName(1)->term_taxonomy_id);

     foreach ($week as $weekday) {
        $PostID = wp_insert_post(array(
            'post_title'    => $latestpost(0)->post_title . ': ' . $weekday,
            'post_type'     => 'palaver',
            'post_parent'   => $latestpost(0)->ID,
            'query_var'     => true
        $PostTitle = $latestpost(0)->post_title . ': ' . $weekday;
        wp_set_object_terms($PostID, $CatTermID, 'gebied');
} add_action('transition_post_status', 'create_days_week', 10, 3);

I also have a custom taxonomy “gebied” but it’s not relevant at present. The registration is in my mu-plugin folder. The palaver posts inherit the week posts’ taxonomy terms.

I have activated the Monkeyman Rewrite Analyzer plugin. The following rewrite_rule


gives post_type: palaver and palaver: (:/((0-9)+))? in the substitution column (no striped through reds). Unfortunately this does not solve it.

I really have no clue how to approach this problem anymore. Since I cannot find anyone else who has a (dynamic) (parent_page)/(child_page) of different post types I’m starting to wonder if it is even possible. As they say I cannot see the wood for the trees…

Any help would be greatly appreciated!
Cheers, B

query variable – Rewrite Rule for Post Meta

I would like to create an endpoint for a custom field. For example, I already have a custom ‘competition’ post type for which rewriting is working great and resolving at Within that competition I have award categories created using Advanced Custom Fields, all of which have a unique ID.

I would like to be able to visit

I’ve got as far as passing the unique ID of the category to the query vars and loading a custom template but am stuck on and can’t figure out how to have my rewrite rule include the parent competition. Here’s my code:

function aca_award_cat_rewrite_rule() {

    add_rewrite_rule( '^award-category/((^/)*)/?', 'index.php?award_category=$matches(1)', 'top' );

function aca_set_award_cat_query_var( $vars ) {

    array_push( $vars, 'award_category' );
    return $vars;

function aca_include_award_cat_template( $template ) {
    if( get_query_var('award_category') ) {
        $award_category_template = plugin_dir_path( dirname( __FILE__ ) ) . 'public/award-category-template.php';
        if( file_exists( $award_category_template ) )
            $template = $award_category_template;
    return $template;

I realise that right now my rewrite_rule is sending the user via index.php but I can’t figure out how to accommodate the awards/award_slug bit without interfering with the existing rewrite rules for the post type.

java – rewrite this code in a more generic way to check for null values ​​and all enumeration values

Basically, I have a request class that has different types of objects, each of which is of type enum.
Based on the request object, I want to write code to iterate through and if a request has a specific enumeration type, add that kind of enumeration or else ignore it.

My main thing is that it needs to be updated every time we add a new value to one of the enumerations. It would also be interesting to only generically check to add objects if they are not null. Is there a way to write this code more efficiently –

class Prefs {
   XEnum xtypes;
   YEnum ytypes;
   // similar enums
//   getters
//   setters
                     public void validate(Prefs prefs) {
                     if (prefs.getXTypes() != null) {
                        List Xtypes = new ArrayList();
                        if(prefs.getXTypes().getTypeX1() != null)){
                        if(prefs.getXTypes().getTypeX2() != null)){
                        if(prefs.getXTypes().getTypeX3() != null)){
                        // Similar Code
                      if (prefs.getYTypes() != null) {
                        List Ytypes = new ArrayList();
                        if(prefs.getYTypes().getTypeY1() != null)){
                        if(prefs.getYTypes().getTypeY2() != null)){
                        if(prefs.getYTypes().getTypeY3() != null)){
                        // Similar Code
                       // call to create a new object based on Xtypes and YTypes