Replace a part of url with htaccess RewriteRule - .htaccess

I have a part of url to replace with htaccess :
How to replace root/aaa/bbb/css/images/ddd.image.png to root/aaa/css/images/ddd.image.png
I tried without success this code in the .htaccess in the root folder :
RewriteEngine On
RewriteRule ^aaa/bbb/(.*) /aaa/$1 [L,QSA,R=301]
In the aaa folder, there is this .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

You need to use this rule in /aaa/.htaccess:
RewriteEngine On
RewriteBase /aaa/
RewriteRule ^bbb/(.+)$ $1 [L,NC,R=301,NE]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]

Related

htaccess with multiple get for activation url

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]

Redirect all url using .htaccess except one dynamic url

I want to redirect all url from http to https, this works just fine for me.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now i want to add one exception to this rule, i.e whenever a user request the following dynamic url (the last string is dynamically generated) it should not apply force https redirect.
sub.domian.dev/api/v/1/download/zip/token/8397298347ksjdnkjasdn0394834
I tried this rule which does not work.
#url to exclude
RewriteCond %{REQUEST_URI} !^/api/v/1$
Can someone give me the pointer on how to go with this?
Thanks.
You can use this code:
RewriteEngine On
# except /api/v/1/ requests redirect everything else to https
RewriteCond %{THE_REQUEST} !\s/+api/v/1/ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/api/v/1/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

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]

htaccess - do not rewrite in this case

My access file looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
But I have a directory called facebook that I want to access as usuall through the URL http://mysite.com/facebook. What do I need to add to the above so that it ignores (does not rewrite) this directory?
add:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !^/facebook
RewriteRule ^.*$ index.php [NC,L]

Resources