Category Archives: mod-rewrite

WordPress ModRewrite does not work?

I have tested this rule on a non-WP-Domain, and it works perfeclty:

RewriteCond %{QUERY_STRING} ^m=(\w+)$
RewriteRule ^dsgvo_gdpr/(.+)$ dsgvo_gdpr/?mail=%1 

I need simply rewrite the querystring-parameter "m" to be "mail", because "m" is a reserved word in wp -.-

but as soon as I place the rule in the .htaccess of WP, it does not work. I have read now, that WP has its own rewrite-system, but whatever I try, it does not work.

Can someone help me to achieve this please? I am dealing with this for weeks now :/

Rewrite which was working with Apache 2.2 doesn’t work on Apache 2.4

I have to migrate my company website to a new Cloud VPS and I'm getting a redirect loop because of the rewrite which was just working fine on old server. It works for some page like Login which was dedicated to be only work with https.

I read a lot already about this and I tried, nothing helped me.

Here is the original rewrite from .htaccess file

    php_flag session.auto_start Off

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]

I found some similar question on this Stack Overflow also - I tried it and it did not work.

How to let .htaccess load pages dynamically when they do not exist using RewriteRule

I am struggling to create an .htaccess file which does the following:

  • Open .php files when the .php extension is not provided
  • Open .php files when they exist
  • Including a language prefix when provided
  • and if the page does not exist, try and search for the page in a database through a file called lookup.php.
  • When all of the above fails, show a 404

Imagine I have these files: index.php, home.php, lookup.php

  • opening URI: localhost/ - will open index.php
  • opening URI: localhost/en/ - will open index.php?lang=en
  • opening URI: localhost/home - will open home.php
  • opening URI: localhost/test - will open lookup.php?page_name=test
  • opening URI: localhost/en/test - will open lookup.php?lang=en&page_name=test

The code I have right now is the following:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME}  !-d
RewriteCond %{REQUEST_FILENAME}  !-f
RewriteCond %{REQUEST_URI} ^(.*){2}/$ [NC]
RewriteRule ^(.*)$ index.php?lang=$1 [L]

RewriteCond %{REQUEST_FILENAME}  !-d
RewriteCond %{REQUEST_FILENAME}  !-f
RewriteCond %{REQUEST_URI} ^(.*){2}/(.*)/$ [NC]
RewriteRule ^(.*)/(.*)$ $2.php?lang=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME}  !-d
RewriteCond %{REQUEST_FILENAME}  !-f
RewriteCond %{REQUEST_URI} ^(.*){2}/(.*/)$ [NC]
RewriteRule ^(.*) lookup.php?lang=$1&page_name=$2 [QSA,L]

RewriteCond %{REQUEST_FILENAME}  !-f
RewriteCond %{REQUEST_FILENAME}  !-d
RewriteRule . 404.php

How to redirect non www to www on subdomain

I want to redirect non www to www in my sub domain. my domain is using wordpress and my subdomain is using native site.

my domain, for example, https://www.example.com and subdomain is https://files.example.com, I want to add www in my subdomain, so my sub domain should be like this https://www.files.example.com.

btw I'm using cloudflare.

my domain(https://www.example.com) .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Options -Indexes

here my subdomain(https://files.example.com) .htaccess code

RewriteEngine On
#Main Redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http%1://www.%{HTTP_HOST}/$1 [R=301,L]

I try this too but still not working

RewriteEngine On
#Main Redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

How to configure correctly VirtualHost?

I have 3 domains, for example example1.com (points to port 8081), example2.com (points to port 8082) and example3.com (points to port 8083). If I enter example1.com (without port), it redirects me to the example page2 but with the domain of example1 (it should not be that way) ... but if I put the port for example1 port 8081, it loads correctly. If in example2 I put the port of 8083, it redirects me to the site of example3 with the domain of example2.

I would appreciate your cooperation.

Next, the virtualhost as they are configured:

example1:

    <VirtualHost *:8082>
    DocumentRoot /var/apps/example1/web
    ServerName example1.com
    ServerAlias *.example1.com
    <Directory "/var/apps/example1/web">
        AllowOverride All
        allow from all
        Options +Indexes
    </Directory>

</VirtualHost>

example2:

<VirtualHost *:8083>
    ServerName example2.com
    ServerAlias *.example2.com

    DocumentRoot /var/apps/example2/web
    <Directory "/var/apps/example2/web">
      AllowOverride All
      allow from all
      Options +Indexes
    </Directory>
</VirtualHost>

example3:

<VirtualHost *:8083>
DocumentRoot /var/apps/example3/web
ServerName example3.com
ServerAlias *.example3.com
<Directory "/var/apps/example3/web">
        AllowOverride All
        allow from all
        Options +Indexes
</Directory>

</VirtualHost>

Thanks.

Trying to rewrite a local url to another

I want to be able to do the following:

ReWrite /test -> /test.php

Also:

ReWrite /Test/test -> /Test/test.php

I want to be able to do this only for those mentioned urls. Also, I do not want direct access to .php urls. For example, if the user navigates to /Test/bla.php or any other links, he would be automatically be directed to the /index. The main reason I am doing this is that I do not want the user to know I am using .php. Here is what I tried:

RewriteEngine On
RewriteRule ^Test/test$ /Test/test.php
RewriteRule ^test$ test.php
ErrorDocument 404 /index.php

This works to some extent. I can navigate to /Test/test and it redirects me to /Test/test.php without issues. Though, if I do something like /test (from the root), I see the Test folder instead. Also, 404 does not work in this .htaccess above. I can navigate to any missing url and it complains that it is not there.

I am new to web hosting. So, I am kind of confused by the .htaccess.

Thanks for your time.

Routing everything to subfolder

I need my htaccess file to redirect all URI calls to my index.php in my /public folder.

This means, that I don't want domain.com/ to access "root", but to redirect to my /public folder.

I actually have it working in some way now with the following:

DirectoryIndex index.php

ErrorDocument 404 /404.html

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteRule ^$    public/    [L]
    RewriteRule (.*) public/$1    [L]
</IfModule>

But my problem is, that I can access ALL files in the /public folder with both domain.com/$1 and domain.com/public/$1

Can I avoid that in any way?

Httpd redirect with proxy throwing ERR_TOO_MANY_REDIRECTS

I have a tomcat app that I am trying to hook into httpd. I have configured the following 2 VirtualHosts...

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName secondave.net
    DefaultType text/html
    ProxyRequests off
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
</VirtualHost>
<VirtualHost *:443>
...
ServerAdmin [email protected]
ServerName secondave.net
DefaultType text/html
ProxyRequests off
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
...
</VirtualHost>

This works great so now I want to redirect users from the http site to the https site. To do this I change the first virtual host to...

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName secondave.net
    Redirect / https://secondave.net
</VirtualHost>

But when I run like this, I get the following...

ERR_TOO_MANY_REDIRECTS

I tried removing the <VirtualHost *:80> and just adding...

RewriteEngine On

# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on

# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

But when I do this the Proxy doesn't work on the ssl and it forces me to the Document Root.

This version works for http://secondave.net but not secondave.net

<VirtualHost *:80>
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
</VirtualHost>
<VirtualHost *:443>
    ...
    ServerAdmin [email protected]
    ServerName secondave.net
    DefaultType text/html
    ProxyRequests off
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
    ...
</VirtualHost>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess rewrite for orphaned URLs containing underscores and arguments

I only modify the .htaccess with great care for the purposes of my online store.

Some time ago, I did a website migration from osCommerce to OpenCart. This resulted in orphaned osCommerce-style URLs with these two example formats:

http://www.londonpower.com/catalog/product_info.php?products_id=75

http://www.londonpower.com/catalog/product_info.php?cPath=15&products_id=75

Lots of websites in internet-land have links to my old-style URLs, and I have about 100 of them, so I would like to redirect them to new URLs with the following format:

http://www.londonpower.com/2-channel-guitar-preamp

If I understand correctly, the problem has two parts:

  1. to eliminate the underscores, as they baffle the .htaccess engine;

  2. to then perform a 301 redirect on the URL.

So far, I have been able to get the first underscore to change to a hyphen, with this Rewrite Rule:

RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]

...but no luck with the second underscore (the one that is part of the query string after the "?"). I am stuck there.

Apache rewrite causes infinite recursion when given a subdirectory

This works fine if the file exists, including if it's in a subdirectory, and it works if the file is at the top level and doesn't exist. However, if a file doesn't exist and it's in a subdirectory, this causes infinite recursion.

    Options +FollowSymLinks
    AllowOverride None
    Require all granted

    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]

This looks very simple to me and I don't know what's going wrong. As far as I can tell it should say: "if the file doesn't exist, and a directory of that name doesn't exist, and if a file name 'x.php' does exist, serve that". Right?

The error log will looks like this until it finally errors out at 10 levels of recursion:

[Wed May 23 08:16:28.176564 2018] [core:debug] [pid 27054] core.c(3620): [client 0.0.0.0:57764] AH00122: redirected from r->uri = /test/test.php.php.php
[Wed May 23 08:16:28.176566 2018] [core:debug] [pid 27054] core.c(3620): [client 0.0.0.0:57764] AH00122: redirected from r->uri = /test/test.php.php
[Wed May 23 08:16:28.176568 2018] [core:debug] [pid 27054] core.c(3620): [client 0.0.0.0:57764] AH00122: redirected from r->uri = /test/test.php
[Wed May 23 08:16:28.176570 2018] [core:debug] [pid 27054] core.c(3620): [client 0.0.0.0:57764] AH00122: redirected from r->uri = /test/test