RewriteRule fails to execute - apache

I've written a rewrite rule to modify this:
https://www.mydomain.com/template.php?staticPage=product.php&code=12345&title=product%20name%20and%description
to this:
https://www.mydomain.com/product.php/12345/product%20name%20and%20description.html
Here's the rule:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /LG_Template.php?staticPage=$1&code=$2&title=$3 [L]
And here it is in context with the rest of the .htaccess file:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule (^includes/.*) $1 [L]
RewriteRule (^static/.*) $1 [L]
RewriteRule ^Topic\ Pages/([^/\.]+)\.htm$ /category.php?oldname=$1 [L]
RewriteRule ^([^/\.]+)\.htm$ /template.php?staticPage=product.php&code=$1 [L]
RewriteRule ^([^/\.]+)\.html$ /template.php?staticPage=product.php&code=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /template.php?staticPage=$1&code=$2&title=$3 [L]
The problem is no rewrite is occurring. I've tried a couple of different options directives at the beginning of the .htaccess I found in posts of similar problems, for example:
Options +FollowSymLinks
Options -MultiViews
Neither of these seems to have any effect on the problem. Any ideas what might be causing this?

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 redirect for all files but with exceptions

I know this has been asked thousands times, but I've been googling for three hours without any result. So I'm asking here. I'm creating a website. All the content is in the folder /subfolder. Now I want to redirect all the requests to another domain (let's say domain.com), at the exception of the files that I actually use. It may seem weird but it makes sense in my situation. So what I have at the moment is
RewriteEngine on
RewriteRule ^account$ account.php
RewriteRule ^home$ myhome.php
RewriteRule ^options$ options.php
RewriteRule ^login$ login.php
RewriteRule ^links$ editlinks.php
RewriteRule ^help$ howto.php
RewriteRule ^$ index.php
RewriteRule ^forgot$ forgot.php
RewriteRule ^r$ redirect.php
RewriteRule ^r/(.*)$ redirect.php?id=$1
RewriteCond %{REQUEST_URI} !(login.php|login)
RewriteRule (.*) external.php?parameter=$1 [L]
How can I do this ? the above code always redirects me to domain.com.
Thank you in advance
Make sure to place redirect just below RewriteEngine On and use THE_REQUEST instead of REQUEST_URI like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} !/media [NC]
RewriteCond %{THE_REQUEST} !/(login|signup)(\.php)?[?\s] [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]
RewriteRule ^home$ myhome.php [L]
RewriteRule ^links$ editlinks.php [L]
RewriteRule ^help$ howto.php [L]
RewriteRule ^(account|login|options|forgot)/?$ $1.php [L,NC]
RewriteRule ^r$ redirect.php [L]
RewriteRule ^r/(.+)$ redirect.php?id=$1 [L,QSA]

mod_rewrite not working for query string redirect

Currently I have one rewrite rule for pretty urls, which is working fine. I tried adding in a second, and it's giving me problems:
# redirect /?p=xyz to /xyz
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ /index.php/%1 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The second one is working fine, but the first one is giving me internal server error. Basically I want anything in ?p=* to go to /index.php/*
[Edit] Just to clarify, I need http://domain.com/?p=test to be equivalent to http://domain.com/test/
domain.com/test to domain.com/?p=test
Try this instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !p= [NC]
RewriteRule ^([^/]+)/? /?p=$1 [L,NC]
OPTION:
In case it is the opposite:
domain.com/?p=test to domain.com/test/index.php
Try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} p=([^/]+) [NC]
RewriteRule .* /%1/index.php? [L,NC]
Remove index.php in case it is not needed, like this:
RewriteRule .* /%1/? [L,NC]

Two level url rewrite with mod_rewrite

I need something like this index.php?page=home to be /home
and
index.php?page=products&cat=chairs to be /products/chairs
What would be the mod_rewrite rule to achieve this?
Edited
I have other pages than home, so my full content of .htaccess file is now
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
But when I navigate to /products/chairs I get a "Object not found" error.
RewriteEngine on
RewriteRule ^/home/?$ /index.php?page=home [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
It rewrites both /home and /home/, both /products and /products/ (you didnt specify which was your intended behavior); beware that you will get an empty cat parameter in those cases.
EDIT: this is a tested .htaccess, with R=301 left on it to see rewriting result, with both .htaccess and index.php in same directory (webroot in my case):
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [R=301,L]
RewriteRule ^products/([^/]+)?/? index.php?page=products&cat=$1 [R=301,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(products)&cat=([^\s]+) [NC]
RewriteRule ^ %1/%2 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(home)\s [NC]
RewriteRule ^ %1 [R=302,L]
RewriteRule ^(products)/([^/]+)/?$ index.php?page=(products)&cat=$1 [L,QSA,NC]
RewriteRule ^(home)/?$ index.php?page=$1 [L,QSA,NC]
Change R=302 to R=301 after you verify it's working fine for you.

REPOST: htaccess Error and Optimization

Can anybody help me optimize and fix my .htaccess file? I'm really bad at regex and I'm not a server person and site I'm building is inaccessible because of the error. Any help would be very much appreciated.
SetEnv _SRVR_ENV beta
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Paypal Callback Rules
RewriteCond %{QUERY_STRING} token=(\w+-\w+)&PayerID=(\w+)
RewriteRule ^some-seo-text-(\w+)-(\w+)\.html /index.php?c=$1&m=$2&token=%1&payerid=%2 [L]
RewriteCond %{QUERY_STRING} token=(\w+-\w+)
RewriteRule ^some-seo-text-(\w+)-(\w+)-(\w+)\.html /index.php?c=$1&m=$2&token=%3 [L]
RewriteCond %{QUERY_STRING} token=(\w+-\w+)
RewriteRule ^some-seo-text-(\w+)-(\w+)\.html /index.php?c=$1&m=$2&token=%1 [L]
RewriteCond %{QUERY_STRING} session=(.*)
RewriteRule ^some-seo-text-(\w+)-(\w+)\.html /index.php?c=$1&m=$2&session=%1 [L]
## Custom Rules
RewriteRule ^some-seo-text-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.html$ /index.php?c=$1&m=$2&v1=$4&v2=$6&v3=$8 [L]
RewriteRule ^some-seo-text-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.html$ /index.php?c=$1&m=$2&v1=$4&v2=$6 [L]
RewriteRule ^some-seo-text-(.*)-(.*)-(.*)-(.*)\.html$ /index.php?c=$1&m=$2&v1=$4 [L]
RewriteRule ^some-seo-text-(.*)-(.*)\.html$ /index.php?c=$1&m=$2 [L]
RewriteRule ^some-seo-text-(.*)-(.*)\.html$ /index.php?c=$1&m=$2 [L]
RewriteRule ^some-seo-text-(.*)\.html$ /index.php?c=$1 [L]
## Directory Cloaking
RewriteRule ^images/another-seo-text-(.*)$ /static/images/$1 [L]
RewriteRule ^deals/another-seo-text-(.*)$ /static/images/campaigns/$1 [L]
RewriteRule ^css/(.*)$ /static/stylesheets/$1 [L]
RewriteRule ^js/(.*)$ /static/javascripts/$1 [L]
RewriteRule ^captcha/(.*)$ /static/captcha/$1 [L]
Note that (.*) will happily match a - in an URL, making many of these matches ambiguous, and perhaps very slow. Your \w matches probably make more sense.
Can you paste the diff(1) between your last known good working .htaccess and this one? That would help you find the fault quickly.