htaccess multipurpose rewrite rules conflict - apache

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]

Related

.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]

Add custom rewrite rule so wordpress won't overide it

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]

mod-rewrite index.php

I have these URLs:
index.php?area=guilds
index.php?area=guilds&page=create
index.php?area=guilds&page=view&name=The+Unit
Could I have only 1 rewrite rule for this somehow?
/guilds
/guilds/create
/guilds/view/The+Unit
Try these rules:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^[^/]+$ index.php?area=$0 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?area=$1&page=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?area=$1&page=$2&name=$3 [L]
The first rule is to avoid that URLs are rewritten that already can be mapped to an existing file. The other rules reflect your three cases.
This one should work :
RewriteRule /(.+)(?:/(.+))?(?:/(.+))? index.php?area=$1&page=$2&name=$3

Rewrite rule not working

I want to write a rule that redirects all URLs of a certain pattern to a PHP file. Exception: the control/ directory where the CMS resides.
Why does
RewriteCond %{REQUEST_URI} ^/([^control]+)/([^/]+)$
RewriteRule .* /pages/index.php?language=%1&page=%2&%{QUERY_STRING} [L]
not work for
domain.com/deutsch/start
(it throws a 404), while
RewriteCond %{REQUEST_URI} ^/([^control]+)/([^/]+)/debug$
RewriteRule .* /pages/index.php?language=%1&page=%2&page_debug=yes&%{QUERY_STRING} [L]
works for
domain.com/deutsch/start/debug
?
You need to use two conditions:
RewriteCond %{REQUEST_URI} !^/control/
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)$
RewriteRule .* /pages/index.php?language=%1&page=%2&%{QUERY_STRING} [L]
But you should use your pattern directly in the RewriteRule directive if possible:
RewriteCond $1 !=control
RewriteRule ^([^/]+)/([^/]+)$ /pages/index.php?language=$1&page=$2&%{QUERY_STRING} [L]
Another hint is to use the QSA flag instead of appending the query explicitly:
RewriteCond $1 !=control
RewriteRule ^([^/]+)/([^/]+)$ /pages/index.php?language=$1&page=$2 [L,QSA]