REPOST: htaccess Error and Optimization - apache

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.

Related

RewriteRule with question marks htaccess

i know this been asked a lot
but i still did not succeeded to do it
i have this in my htaccess
RewriteRule ^embed/([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
i want it to be
RewriteRule ^embed/?v=([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/?v=([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
adding ?v=
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/%1([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [NC,L]
RewriteRule ^embed/%1([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [NC,L]
not working
example
mysite.com/embed/?v=abcde
mysite.com/embed/?v=abcde/6x6
currently it is
mysite.com/embed/abcde
mysite.com/embed/abcde/6x6
so now this work
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1 [L]
i have noticed that i have this in the file
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_CGI_AUTHORIZATION:%1]
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^$ /cgi-bin/index_dl.cgi?op=download1&id=%1 [NC,L]
i am still trying to make this to work
RewriteCond %{QUERY_STRING} ^v=(.*)/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=$2&h=$3 [L]
You are not using the rules properly - passing %1 to the rules won't help you, because you're not matching correctly. REQUEST_URI and QUERY_STRING are two different things. RewriteRule is only able to check the REQUEST_URI, and not the QUERY_STRING, as shown in the answer suggested by Panama Jack.
To achieve what you want, you need to use the following instead:
RewriteEngine On
# Check for mysite.com/embed/?v=abcde/6x6
RewriteCond %{QUERY_STRING} ^v=([^&/]+)\/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=%2&h=%3 [L]
# Check for mysite.com/embed/?v=abcde
RewriteCond %{QUERY_STRING} ^v=([^&]+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=%1 [L]
Here, we simply check the query string for each case. If there is a match, only rewrite if we're at /embed/ (trailing slash optional - you may remove the question mark if you wish for the trailing slash to be required).

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

RewriteRule fails to execute

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?

htaccess rewrite virtual folder

im trying to create website, and i need to rewrite php file switch cases to virtual directories.
Im trying to make url of type http://localhost/en/dashboard/mailbox/send/username
to work, and its working with the following code
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2})- [NC]
## Rewrite Rules
RewriteRule ^/?$ /%1/%{REQUEST_URI} [L,NC,R]
RewriteRule ^([a-z]{2})/?$ /index.php?language=$1 [L,NC,QSA]
RewriteRule ^account/?$ /%1%{REQUEST_URI} [L,NC,R]
RewriteRule ^([a-z]{2})/(account)(?:/([^/]+)(?:/([^/]+))?)?/?$ account.php?language=$1&action=$3 [L,NC]
RewriteRule ^dashboard/?$ /%1%{REQUEST_URI} [L,NC,R]
RewriteRule ^([a-z]{2})/(dashboard)(?:/([^/]+)(?:/([^/]+))(?:/([^/]+))?)?/?$ /$2.php?language=$1&type=$3&subtype=$4&lasttype=$5 [L,NC,QSA]
However, from now i cannot access http://localhost/en/dashboard/mailbox/ itself
Can someone help me to solve this problem?
Im not into this htaccess thing at all, but i did so far all i could (i think so)
Thanks to #hjpotter92, this problem was solved.
I missed one question mark, so thats why this code was kind of broken.
Here is correct string:
^([a-z]{2})/(dashboard)(?:/([^/]+)(?:/([^/]+))?(?:/([^/]+))?)?/?$
All i missed is
^([a-z]{2})/(dashboard)(?:/([^/]+)(?:/([^/]+))?(?:/([^/]+))?)?/?$
And here is fully correct code
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2})- [NC]
## Rewrite Rules
RewriteRule ^/?$ /%1/%{REQUEST_URI} [L,NC,R]
RewriteRule ^([a-z]{2})/?$ /index.php?language=$1 [L,NC,QSA]
RewriteRule ^account/?$ /%1%{REQUEST_URI} [L,NC,R]
RewriteRule ^([a-z]{2})/(account)(?:/([^/]+)(?:/([^/]+))?)?/?$ account.php?language=$1&action=$3 [L,NC]
RewriteRule ^dashboard/?$ /%1%{REQUEST_URI} [L,NC,R]
RewriteRule ^([a-z]{2})/(dashboard)(?:/([^/]+)(?:/([^/]+))?(?:/([^/]+))?)?/?$ /$2.php?language=$1&type=$3&subtype=$4&lasttype=$5 [L,NC,QSA]

where to set gwt(Google webmater tool) verification in cakephp

//here is my htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^glamestates\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.glamestates.co.uk%{REQUEST_URI} [R=301,L] # <-- Mind the 'L'!
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
where about to add this google webmaster tools link in my htaccess
glamestates.co.uk/googlec7feca3a4513beef.html
what I have got so far but this doesn't work. I added in line five
RewriteRule ^glamestates\.co\.uk\googlec7feca3a4513beef\.html$ http://www.glamestates.co.uk\googlec7feca3a4513beef.html%{REQUEST_URI} [R=301,L]
I have already put this question here since many day nobody answered
Still not 100% clear about what you're asking, but I think perhaps you want this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^glamestates\.co\.uk$ [NC]
RewriteRule ^googlec7feca3a4513beef\.html$ $0 [L]
RewriteRule ^.*$ http://www.glamestates.co.uk%{REQUEST_URI} [R=301,L] # <-- Mind the 'L'!
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
just need to paste the googlec7feca3a4513beef.html file into Webroot folder rather than Root