Add custom rewrite rule so wordpress won't overide it - apache

This is my htaccess file, wordpress multisite htacces. I need to put my custom rule in there and not let wordpress aoverride it. How can i do that? The bold one is my custom rewrite rule.
RewriteEngine On
RewriteBase /
**MY CUSTOM RULE RewriteRule ^/\?currentpage=(\d+)$ currentpage/$1.html [NC,L]**
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

With RewriteRule you can only check the URI path and not the query. You need to use RewriteCond and QUERY_STRING for the URI query:
RewriteCond %{QUERY_STRING} ^currentpage=(\d+)$
RewriteRule ^$ currentpage/%1.html [NC,L]

Related

Htaccess : Redirect OK, but rewrite doesn't work

I have fairly simply rewrite rules :
# Evenements URL rewrite
RewriteRule ^agenda/evenement/([^/]+)-([0-9]+)$ agenda/evenement/?idEvent=$2&title=$1 [R=302,QSA,L]
RewriteRule ^en/agenda/evenement/([^/]+)-([0-9]+)$ en/agenda/evenement/?idEvent=$2&title=$1 [R=302,L]
RewriteRule ^en/calendar/event/([^/]+)-([0-9]+)$ en/calendar/event/?idEvent=$2&title=$1 [R=302,L]
That works out fine. But I want to rewrite, not redirect. If I remove the R=302, I get 404s automatically. Any idea why ?
Here's the full block, to see if my rules are at the wrong place (I've tried to move them around)
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
# Evenements URL rewrite
RewriteRule ^agenda/evenement/([^/]+)-([0-9]+)$ agenda/evenement/?idEvent=$2&title=$1 [R=302,QSA,L]
RewriteRule ^en/agenda/evenement/([^/]+)-([0-9]+)$ en/agenda/evenement/?idEvent=$2&title=$1 [R=302,L]
RewriteRule ^en/calendar/event/([^/]+)-([0-9]+)$ en/calendar/event/?idEvent=$2&title=$1 [R=302,L]
RewriteRule . index.php [L]
</IfModule>
Thanks !

.htaccess lowercase rewrite causes HTTP error

I'm trying to write an .htaccess rewrite based on modified code from here that will convert URLs like this:
http://eXAMplE.CoM/sOMe-PatH/aNOTHer-PAth/FILEName.eXT
To this:
http://example.com/some-path/another-path/FILEName.eXT
Basically converting the entire URL to lowercase and excluding the filename.
[PROBLEM]
My .htaccess seems to be working OK with one exception: When anything beyond the initial path includes a capital letter, it results in no data from the server. I assume it's causing an infinite loop but can't be sure because I can't access the logs.
To clarify, this works:
http://eXAMplE.CoM/sOMe-PatH/FILEName.eXT
This does not:
http://eXAMplE.CoM/sOMe-PatH/aNOTHer-PAth/FILEName.eXT
Here is my code. Can anyone help?
RewriteEngine On
RewriteBase /
# Skip this entire section if no uppercase letters in requested URL
RewriteRule !.*?[A-Z].*\/.* - [S=28]
# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*\/.*)$ $1a$2
RewriteRule ^([^B]*)B(.*\/.*)$ $1b$2
RewriteRule ^([^C]*)C(.*\/.*)$ $1c$2
RewriteRule ^([^D]*)D(.*\/.*)$ $1d$2
RewriteRule ^([^E]*)E(.*\/.*)$ $1e$2
RewriteRule ^([^F]*)F(.*\/.*)$ $1f$2
RewriteRule ^([^G]*)G(.*\/.*)$ $1g$2
RewriteRule ^([^H]*)H(.*\/.*)$ $1h$2
RewriteRule ^([^I]*)I(.*\/.*)$ $1i$2
RewriteRule ^([^J]*)J(.*\/.*)$ $1j$2
RewriteRule ^([^K]*)K(.*\/.*)$ $1k$2
RewriteRule ^([^L]*)L(.*\/.*)$ $1l$2
RewriteRule ^([^M]*)M(.*\/.*)$ $1m$2
RewriteRule ^([^N]*)N(.*\/.*)$ $1n$2
RewriteRule ^([^O]*)O(.*\/.*)$ $1o$2
RewriteRule ^([^P]*)P(.*\/.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*\/.*)$ $1q$2
RewriteRule ^([^R]*)R(.*\/.*)$ $1r$2
RewriteRule ^([^S]*)S(.*\/.*)$ $1s$2
RewriteRule ^([^T]*)T(.*\/.*)$ $1t$2
RewriteRule ^([^U]*)U(.*\/.*)$ $1u$2
RewriteRule ^([^V]*)V(.*\/.*)$ $1v$2
RewriteRule ^([^W]*)W(.*\/.*)$ $1w$2
RewriteRule ^([^X]*)X(.*\/.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*\/.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*\/.*)$ $1z$2
# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule .*?[A-Z].*\/.* - [N]
# Force a redirect with the new lowercase URL.
RewriteRule (.*) $1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
</IfModule>
You'll be better off using RewriteMap instead. In the server configuration (or VHost configuration) add the following:
RewriteMap to_lower int:tolower
Now, you can use the function to_lower in your rewrite rules. Give the following a try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?=[A-Z]).+)(/[^/]+)$ /${to_lower:$1}$2 [R=301,L]

.htaccess URL re-write - strip .php suffix

I want to rewrite URLs like this:
http://domain.com/category.php?id=xx
to:
http://domain.com/category/?id=xx
Also, I want to hide the index.php. How can I achieve this with .htaccess? The following code doesn't work:
##REDIRECTS-START
##REDIRECTS-END
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(ADMIN.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
### ROOT DIR TO PROJECT
RewriteRule . wallkill/index.php [L]
Thanks.
Try this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
reference link
You need 2 sets of rules. The first set redirects the browser to requests without the .php at the end, then internally rewrites the .php back. The second rule redirects the browser to requests without the index.php.
So something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(.*)index\.php
RewriteRule ^ /%1 [L,R]
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php
RewriteRule ^ /%1/ [L,R]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Note that when you add that / trailing slash to the end, your relative URL base changes, and all your relative links will break. To fix this, either make sure all your links are absolute (start with / or http://) or add a relative base to the header of all your pages:
<base href="/" />

htaccess on multidomain setup: other domains don't work

I have a multidomain setup that looks like this on my server … this is a model of the structure:
.htaccess
***my-website
.htaccess
index.php
projects.php
jobs.php
some-project.php
another-project.php
some-job.php
another-job.php***
other-domain
other-domain
other-domain
I have two goals:
1.) The root .htaccess file should just make sure, that my main-domain which is /my-website is run/executed automatically when entering www.my-website.com. Due to the fact that I have a multidomain ftp-server I need do specify that. That is working already.
2.) In my sub-directory /my-website I need some different rules, especially regarding better url design and the omittance of .php extension.
So in /my-website I want to neglect all .php suffixes.
And I want to "fake" directories like /jobs /projects which are actually not directories but php-files themselves.
This is what I have so far thanks to #anubhava in this thread.
DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
/my-website/.htaccess:
RewriteEngine On
RewriteBase /my-website/
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/my-website/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
However I have the problem now when using this, that in my-website the links without a trailing .php wont work. I want my links to work like /projects but run/exectute /projects.php in my-website. However this doesn't work at the moment, i get a 404.
I also want /projects/some-project to run/execute /some-project.php (where the projects directory doesn't actually exist) which doesn't work either, also 404.
So, to sum it up.
All my domains work now! So my multidomain-setup is save and handeld.
However inside /my-website I want all links to work without trailing .php and with this "faked" directories
So right now this works …
www.my-website.com // works
www.other-domain.com // works
www.my-website.com/projects // doesn't work without trailing .php
www.my-website.com/projects.php // works (runs projects.php)
www.my-website.com/projects/some-project // doesn't work (should run some-project.php)
www.my-website.com/projects/some-project.php // works (runs some-project.php)
Have root .htaccess like this:
RewriteEngine On
RewriteBase /
## COMMENT out these 2 rules
# RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
# RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]
# add .php extension
RewriteCond %{DOCUMENT_ROOT}/my-website/$1\.php -f [NC]
RewriteRule ^(?:[^/]+/)?([^/]+)/?$ my-website/$1.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?my-website\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/my-website/ [NC]
RewriteRule ^(.*)$ my-website/$1 [L]
# add .php extension 2
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(?:[^/]+/)?([^/]+)/?$ $1.php [L]
/my-website/.htaccess:
RewriteEngine On
RewriteBase /my-website/
# add .php extension
RewriteCond %{DOCUMENT_ROOT}/my-website/$1\.php -f [NC]
RewriteRule ^(?:[^/]+/)?([^/]+)/?$ $1.php [L]
DocumentRoot/.htaccess:
RewriteBase /
RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]
# add .php extension for REQUEST_FILENAME existing in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# Check if REQUEST_URI is existing as file or directory in /my-website
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
# Check if REQUEST_URI is existing as file.php in /my-website
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ /my-website/$1 [L]
/my-website/.htaccess:
RewriteEngine On
RewriteBase /my-website/
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

htaccess multipurpose rewrite rules conflict

I have 2 simple PHP files:
product.php
product-options.php
And in my htaccess file i have:
RewriteEngine On
RewriteRule ^product$ product.php [L]
RewriteRule ^options$ product-options.php [L]
RewriteRule ^product-([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^options-([^/]+)/?$ product-options.php?id=$1 [L]
Everything works fine when i call "mysite.com/product" or "mysite.com/product-MYID"
But when i call "mysite.com/options" or "mysite.com/option-MYID" i see the correct url but the content of the file is "product" / "product-MYID" !!! Why???
PS: i need that both files are accessibile with and without the GET ?id=ID
Or you can use:
RewriteEngine On
RewriteRule ^product$ product.php [L]
RewriteRule ^options$ product-options.php [L]
RewriteRule ^product-([^/\.]+)/?$ product.php?id=$1 [L]
RewriteRule ^options-([^/\.]+)/?$ product-options.php?id=$1 [L]
because, I think, you don't use . in your product-... or option-...
That is because your rules are executing 2nd time as regex is:
^product-([^/]+)/?$
Which matches /product-MYID URI as well as rewritten URI: product-options.php
In order to avoid this you can use these rules:
RewriteEngine On
# If the request is for a valid directory or file then skip rewrite rules
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^product/?$ product.php [L]
RewriteRule ^options/?$ product-options.php [L]
RewriteRule ^product-([^/]+)/?$ product.php?id=$1 [L,QSA]
RewriteRule ^options-([^/]+)/?$ product-options.php?id=$1 [L,QSA]