After a lot of research I'm still struggling.
My ./.htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/trackparser/.*$
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
However, if I go to www.mysite.com/trackparser it just gives me a missing controller error.
How do I make it ignore the trackparser directory?
Besides the required / as mentioned by #GregSchmidt, rewrite conditions only apply to the first rule that is following them, so your config basically says:
IF (URI != ^/trackparser/.*$) {
RewriteRule ^(\.well-known/.*)$ $1 [L]
}
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
What you want is to apply all three rules if the URI doesn't match ^/trackparser/.*$. To achieve that you can invert the condition and use a skipping rule, which when applied, will skip N number of the subsequent rules:
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteCond %{REQUEST_URI} ^/trackparser(/.*|$)
RewriteRule . - [S=2]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
This will skip the 2 CakePHP webroot rules in case the condition is met, so it's basically doing:
RewriteRule ^(\.well-known/.*)$ $1 [L]
IF (URI != ^/trackparser(/.*|$)) {
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
}
Note the removed negation operator, and the added (/.*|$) to make the ending slash optional. The rule for the well-known directory (certificate validation) could theoretically also be skipped, but I'd make it exempt in order to avoid it accidentally not being applied because of later changes.
See also
Apache > HTTP Server > Documentation > Version 2.4 > Modules > Apache Module mod_rewrite > RewriteCond Directive
Apache > HTTP Server > Documentation > Version 2.4 > Rewrite > RewriteRule Flags > S|skip
Related
##### PART 1
# IF:
RewriteRule ^ac$ /ac05182018.pdf [R=301,NC,L]
# THEN:
RewriteCond %{DOCUMENT_ROOT}/downloads/$1 -f
RewriteRule ^(.+\.(pdf))$ downloads/$1 [L]
##### PART 2
# IF:
RewriteRule ^ac64$ /ac64_05182018.pdf [R=301,NC,L]
# THEN:
RewriteRule ^(.*).(pdf)$ download.php?file=$1.$2 [L]
"PART 1" works standalone (= if one requests http...mysite.com/ac then this rewrites to "http...mysite.com/downloads/ac05182018.pdf").
"PART 2" works standalone as well (= if one requests http...mysite.com/ac64 then this rewrites to "http...mysite.com/download.php?file=ac64_05182018.pdf").
I was wondering how to achieve the following:
I need both of them (PART 1 & 2) to be combined, so that either one will be executed, I mean that both work, depending on what file gets requested (either /ac or /ac64).
When I do so though, "PART 1" no longer rewrites to "/downloads/ac05182018.pdf" and then stops executing the rest. Instead it rewrites to "http...mysite.com/download.php?file=downloads/ac05182018.pdf"...
Any idea how to achive this?
Something like:
# IF:
RewriteRule ^ac$ /ac05182018.pdf [R=301,NC,L]
# THEN:
RewriteCond %{DOCUMENT_ROOT}/downloads/$1 -f
RewriteRule ^(.+\.(pdf))$ downloads/$1 [L]
# ELSE
# IF:
RewriteRule ^ac64$ /ac64_05182018.pdf [R=301,NC,L]
# THEN:
RewriteRule ^(.*).(pdf)$ http...mysite.com/download.php?file=$1.$2 [L]
Maybe with RewriteRule flag [S] or
<If ... >
...
</If>
<Else>
...
</Else>
as both seen in the answers of How to write "if...else" mod_rewrite statements in .htaccess ?
First this line RewriteCond %{DOCUMENT_ROOT}/downloads/$1 -f will prevent next rule RewriteRule ^(.+\.(?:jpe?g|pdf))$ downloads/$1 [L] to be executed because you don't have downloads yet in document root .
After remove it , there will be two rules to .pdf files :
RewriteRule ^(.+\.(?:jpe?g|pdf))$ downloads/$1 [L]
and this :
RewriteRule ^(.*).(rar|zip|pdf)$ http...mysite.com/download.php?file=$1.$2 [R,L]
Unless you do some conditions one of them both will match .pdf and that will give error .
Moreover , don't forget to to put L flag like here :
RewriteRule ^ac64$ /ac64_05182018.pdf [R=301,NC]
it should be RewriteRule ^ac64$ /ac64_05182018.pdf [R=301,NC,L]
L flag means if the rule matches, no further rules will be processed.
If i understand you your code should be like this :
RewriteEngine On
#Part 1
RewriteRule ^ac$ /ac05182018.pdf [R=302,NC,L]
RewriteCond %{REQUEST_URI} !(downloads|ac64)
RewriteRule ^(.+)\.(jpe?g|pdf)$ /downloads%{REQUEST_URI} [L]
#part 2
RewriteRule ^ac64$ /ac64_05182018.pdf [R=302,NC,L]
RewriteCond %{REQUEST_URI} !downloads
RewriteRule ^(.+)\.(rar|zip|pdf)$ /download.php?file=$1.$2 [L]
Clear browser cache and test it , if it is ok , change any 302 to 301 to get permanent redirection .
Note: any .pdf files not start with ac64 will be treated by the ac rule , first rule .
Here are the .htaccess rules:
RewriteEngine On
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap.xml$ xmlsitemap.php [L]
Options -Indexes
Here every URL is pointing to content.php?seourl=$1, even css, js and image files.
Here are some example URLs I need,
http://example.com/sjskjfsk21
http://example.com/asfasfasf43sf
http://example.com/pdf/fhfdhdh3432aaf
http://example.com/pdf/aisfyiahm2faf3
http://example.com/browsepdf
http://example.com/browsepdf-1
http://example.com/browsepdf-2
http://example.com/download/fjaskfjalsk3rs
http://example.com/download/usaydiy7aisydi
http://example.com/sitemap.xml
Can anyone please fix the .htaccess file.
Here every url is pointing to "content.php?seourl=$1"
Because your first (generic) rule catches all the requests. You need to change the order so you have the most specific rules first, and the most generic (catch-all) rules at the end. In your case you just need to move the first rule to the end. For example:
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
NB: I backslash-escaped the dot in sitemap.xml to match a literal dot, otherwise it matches any character.
even css, js and image files.
You can make an exception for these static resources at the beginning of your file, before the existing directives. For example:
RewriteRule \.(js|css|png|jpg|gif)$ - [L]
For any URL that ends in any of the stated file extensions then stop processing the current mod_rewrite rules.
Alternatively (although perhaps marginally less efficient), you can prevent processing of requests for files that exist. Again, this goes before your existing mod_rewrite directives. For example:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
However, this must now check every request for the existence of a file on the filesystem that maps to the request. (It could also be combined with the above rule if required.)
UPDATE: Bringing this together, we have:
# Exclude any existing files from being rewritten
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Specific rewrites
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
# Any other requests for the form "/<anything>"
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).
Some global conditions (for all the rules)
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /
RewriteRule ^(nl|en)$ $1/ [NC,R]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php
Some conditions for the rewrite + folder
RewriteRule ^test/$ test.php
The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.
For the second redirect (the version without the trailing slash) I can't get this working.
RewriteRule ^test$ test.php
Now my URL is rewritten as www.example.com/test/?language=nl
Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.
This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.
RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]
NOTES:
The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).
Full .htaccess:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]
RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]
NOTES:
There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.
My current .htaccess file looks like this:
RewriteEngine on
RewriteBase /
Options +FollowSymLinks -Indexes
RewriteRule ^video/(.*)$ video.php?id=$1 [L]
RewriteRule ^video/(.*)$([a-zA-Z0-9]+) video.php?id=$1 [L]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)/page-(.*)$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)?$ tag.php?tag=$1
RewriteRule ^page/(.*)$ page.php?id=$1 [L]
RewriteRule ^feed feed.php [L]
i want to add a slash to all my url's
like this:
> example.com/video/video_id/
>
> example.com/tag/keyword/
>
> example.com/tag/keyword/page-2/ (3... and so on...)
>
> example.com/page/name/
>
> example.com/feed/
And i want to redirect my current links to the new slash url's
Can somebody help me, please?
Your current htaccess file supports a trailing / although you probably would prefer
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]
So that you don't have to handle the / in video.php
Just update all of your URLs to be example.com/video/video_id/ instead of example.com/video/video_id in whatever you are using (your framework/flat HTML files).
Your old URLs will still work. If you really want to redirect them, you can:
RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video.php?id=$1 [L,R=301]
The [NC] means No-case checking (so /VIDEO) would work. The [R=301] means permanent redirect (useful for SEO).
Go through and expand for your other rules.
Edit:
Sorry, I don't think it was quite right before. Try the following:
RewriteEngine on
RewriteBase /
Options +FollowSymLinks -Indexes
RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video/$1/ [L,R=301]
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]
RewriteCond %{REQUEST_URI} ^tag/ [NC]
RewriteRule ^tag/(.*)/page-(.*)$ tag/$1/page-$2/ [L,R=301]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
...
I have a cakephp installation in the root of my domain. Now it turns out I need to put another app in there that will reside in a subdirectory. How do I disable the controller/model redirection in cake for just this directory?
The current .htaccess in the root folder looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I've tried modifying it like this, but to no avail:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bildbank$ /bildbank/ [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I am aware that this is a bit of a hack, but there's no way I can get the second app to play nice with cake.
Deizel's answer didn't work for me, but placing the following RewriteRule above the two cakephp rules did:
RewriteRule ^bildbank - [L]
The following rule rewrites www.example.com and www.example.com/ to www.example.com/app/webroot/
RewriteRule ^$ app/webroot/ [L]
This rule rewrites www.example.com/* to www.example.com/app/webroot/*
RewriteRule (.*) app/webroot/$1 [L]
I would throw out your rule and update the wildcard regex in the last rule, (.*), so that it matches any string which doesn't start with bildbank. Something like this might do the trick:
RewriteRule ((?!bildbank).*) app/webroot/$1 [L]
This converts the following strings:
cake
cake.htm
cake/test
bilder
bilder.htm
bilder/test
bildbank
bildbank.htm
bildbank/test
.. into:
app/webroot/cake
app/webroot/cake.htm
app/webroot/cake/test
app/webroot/bilder
app/webroot/bilder.htm
app/webroot/bilder/test
bildbank
bildbank.htm
bildbank/test
.. therefore, excluding your bildbank subdirectory.