wordpress: mod_rewrite first "folder" name as a get parameter - apache

I set the wordpress permalink-structure to just use the articlename and got a .htaccess that looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
works fine.
but I want to distinguish between different locations (i.e. berlin, hamburg or muenchen) in my various template files. so I thought I'd add the location to the URL like this:
http://myurl.com/berlin/articlename
and now I need to rewrite this to
http://myurl.com/articlename?location=berlin
but
http://myurl.com/articlename
or
http://myurl.com/category/articlename
should still work. just look for predefined locations (berlin|hamburg|muenchen).
how do I need to adjust the RewriteRule above to accomplish this?

So basically if what you're asking for is that http://myurl.com/(word1)/(word2)/ (i.e. only matches exactly two words, no less no more) gets internally rewritten on the server side to http://myurl.com/(word2)?location=(word1) then here it is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule /([^/]+)/([^/]+)/$ /$2?location=$1 [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

i fixed this with the following additions to the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !location
RewriteRule ^(muenchen|berlin)/$ /index.php?location=$1 [NC,QSA,P]
RewriteCond %{QUERY_STRING} !location
RewriteRule ^(muenchen|berlin)/(.*)$ /$2?location=$1 [NC,QSA,P]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related

Non-www RewriteRule not working on request URI

I need a website to have all URLs NOT have www in front of it. For this, I have found some useful bits of code here that have helped me out a lot. However, nowhere can I find how to properly do it for ALL request URIs. Currently it only works on the domains homepage.
# 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]
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^(.*)$ https://domain.nl%{REQUEST_URI} [L,R=301]
</IfModule>
# END WordPress
Right now www.domain.nl gets rewritten into domain.nl. However, any other like www.domain.nl/anything_else do not get rewritten (also tried adding a / before the %{REQUEST_URI}, did not work).
Any help is appreciated!
With www rule before:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^ https://domain.nl%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

htaccess error leading to loop

I am trying to force SSL on HTACCESS file and all my efforts result in a loop on index.php.
This is my htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My site works fine with this but no SSL.
I tried the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ https://www.fakedomainlollol.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The updated rewrite rule sends the site in a loop.
What is the error? What am I doing wrong here?

Append query string to all URLs with .htaccess

For testing purposes, I would like to append a query string (let's say testing=true) to any url requested from my website.
E.g. I want to change
example.com
example.com/product
example.com/search?q=string
to
example.com?testing=true
example.com/product?testing=true
example.com/search?q=string&testing=true
I tried this:
RewriteRule ^/?(.*) http://example.com/$1?testing=true [L,R,NE]
but it breaks the site. Not an expert.
Any ideas?
EDIT:
I should add I'm working with CodeIgniter (PHP) and my current .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
You can use this new rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)testing=true(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?testing=true [L,QSA,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

RewriteBase not being applied

I can't seem to get my .htaccess to work, I'm trying to rewrite all urls that aren't real files or folders to my index.php. The directory I'm in is /cms the rewrite only seems to work when I use this .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cms/index.php [L]
</IfModule>
When I try and use the RewriteBase /cms/ and change the last line to . /index.php [L] like it's meant to be, the rewrite doesn't rewrite to localhost/cms/index.php and instead it gets rewritten to localhost/index.php
Try using this version:
RewriteEngine On
RewriteBase /cms/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Note that I'm using index.php instead of /index.php with RewriteBase to make sure relative URI is loaded.

mod_rewrite check public folder before passing to php

What I am trying to achieve (with mod_rewrite) is on request check if a file/directory exists in the public folder, if it does, serve it, if not, route the request to the index.php file. Also if no extension provided in the URL then default to .html.
Below is my folder structure:
/.htaccess
/index.php
/public
/test.html
/test.xml
/a_folder
/index.html
/test.html
So for example here are a few requests and responses:
example.com/test.xml >>> /public/test.xml
example.com/a_folder/ >>> /public/a_folder/index.html
example.com/a_folder/test >>> /public/a_folder/test.html
example.com/not_in_public >>> /index.php
Any pointers on this would be amazing, thanks in advance.
Something like this: (Heavily cribbed from my WordPress .htaccess file)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)^(\.\w+) $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The $1.html line is off the top of my head and may need some tweaking. Note that this is a dumb check that the requested URL ends in a dot followed by one or more word characters.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(|index\.php)$ - [L]
RewriteRule ^public/(.*)$ - [L]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [R]
</IfModule>
This should do the trick.
Basically, if it's / or /index.php it won't do anything, if it's /public/whateveryouwant it won't do anything, if it's anything else it will rewrite to /public/whateveryouwant and check if it's either a file or a directory. If it's not it will redirect to index.php.
Thanks to #jxpx777 and #N1xx1 after a bit of juggling made it work.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^public/(.*)$ - [L]
# Check public cache
RewriteRule ^(.*)$ public/$1
RewriteRule (.*)/$ $1/index
# Add .html if not extension provided
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^(.*)$ $1.html
# Push through stack in not in cache
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]