htaccess with multiple get for activation url - .htaccess

I have this following htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^(.*)/ index.php?p=$1
RewriteRule ^(.*) index.php?p=$1
This allows me to do the following:
domain.com/activate/
I wanted to add an 2nd parameter: domain/activate/aSdxa2osd1plD
where index.php?p=activate&h=aSdxa2osd1plD
I tried several htaccess files, but they don't seem to work.
Can anyone help me please?

You can use these rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# to handle domain/activate
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
# to handle domain/activate/aSdxa2osd1plD
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p=$1&h=$2 [L,QSA]

Related

htaccess rewrite and pass top directory name

Below works for passing directory names while doing a htaccess rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) /index.php?xa=$1&xb=$2&xc=$3&xd=$4 [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /index.php?xa=$1&xb=$2&xc=$3 [NC]
RewriteRule ^([^/]+)/([^/]+) /index.php?xa=$1&xb=$2 [NC]
However when adding to above, the final line to also catch server.com/whatever situations:
RewriteRule ^([^/]+) /index.php?xa=$1 [NC]
I get a 500 server error...
How come?..
Thanks!
There are 2 problems:
RewriteCond before first rule is being applied to very next RewriteRule only
No anchors $ in your regex for RewriteRule.
Here is the fixed code:
RewriteEngine On
# ignore all rules below from files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?xa=$1&xb=$2&xc=$3&xd=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?xa=$1&xb=$2&xc=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?xa=$1&xb=$2 [L,QSA]

How would I add 301 redirect to htaccess without fouling up my rewrite conditions?

I need to update my htaccess file because of a change in SEO. The update is from this style domain.org to www.domain.org. So there needs to be a 301 update to the file.
How would I add 301 redirects? Also, do I need to change the index.html sections to www.index.html along with the 301 addition?
This is what my htaccess looks like now.
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.html?x=$1&y=$2&z=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.html?x=$1&y=$2 [L]
RewriteRule ^([^/]+)/?$ index.html?x=$1 [L]
I've fouled up htaccess in the past, so I appreciate the help.
You can use:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.html?x=$1&y=$2&z=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.html?x=$1&y=$2 [L,QSA]

htaccess rules to skip files and directory but checking by matching with a prefix first

I am using following rules which skips files if paths to them is already provided.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^$ public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.+)$ public/index.php?r=$1 [L,QSA]
But this makes me, to access the files like site.com/public/js/somefile.js, which is not intented.
Can we check if file exists inside a directory and rewrite the url so to access it as `site.com/js/somfile.js"
RewriteCond %{REQUEST_FILENAME} -"public/"f
RewriteRule ^ - [L]
Obvioulsy this didn't work. Any ideas?
Right before this rule:
RewriteRule ^$ public/index.php [NC,L]
Add this rule:
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d
RewriteRule ^(.+)$ /public/$1 [L]

Looking for advice on mod_rewrite and htaccess

I am not experienced with htaccess... But I am authoring a site on which I am using a simple redirect. I basically just copied the first htaccess that I found and it works fine, but I am curious if I am using it properly and/or if it is responsible for some problems I am having with the site.
This is my file..
RewriteEngine On
Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /src/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
The index.php (as you can tell) lives in /src), as does most pf the site.
One problem I am having is that accessing some of the includes results in pulling back the index.php!
Any advice, or directions to some tuts or articles on htaccess would be GREATLY appreciated.
Thanks
Have to say that the lot of the code in here looks redundant. Replace your code with this:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} ^/(dir1|dir2|dir3) [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1 [L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
Also when you say that ccessing some of the includes results in pulling back the index.php! I hope you are just including them like:
include('path/to/somefile.php');
That won't go through .htaccess since this inclusion is transparent to Apache.
Try combining a few of the rules:
RewriteEngine On
# Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
# block access to the htaccess file
RewriteRule ^\.htaccess$ - [F,L]
# route "/" requests to index.php
RewriteRule ^$ /src/index.php [L]
# route requests that exists in /src/ to /src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -d
RewriteRule ^(.*)$ /src/$1 [L]
# route everything else to /src/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /src/index.php [L]

.htaccess - Dynamic Redirect virtual folder to file

I have the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [S=44]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=44]
RewriteRule ^(.*)$ index.php?id=$1
It redirects site.com/folder to site.com/index.php?id=folder
I would like it to redirect site.com/folder/?lol=some to site.com/index.php?id=folder&lol=some
Use QSA flag. Change your last RewriteRule to:
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]

Resources