htaccess combine folder with alias - .htaccess

I have 3 folders in a sub directory of my root directory i would like to combine.
My site map:
mywebsite.com
mywebsite.com/test/
mywebsite.com/test/one/
mywebsite.com/test/one/one.php
mywebsite.com/test/two/two.php
mywebsite.com/test/three/three.php
I would like to access:
mywebsite.com/test/one/one.php
mywebsite.com/test/two/two.php
mywebsite.com/test/three/three.php
by using:
mywebsite.com/test/combine/one.php
mywebsite.com/test/combine/two.php
mywebsite.com/test/combine/three.php
Solution...
RewriteEngine On
RewriteRule .* - [E=directory:/test/]
RewriteRule .* - [E=one:one/]
RewriteRule .* - [E=two:two/]
RewriteRule .* - [E=three:three/]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:one}$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:one}$1 -d
RewriteRule ^combine/(.*)$ %{ENV:directory}%{ENV:one}$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:two}$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:two}$1 -d
RewriteRule ^combine/(.*)$ %{ENV:directory}%{ENV:two}$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:three}$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:three}$1 -d
RewriteRule ^combine/(.*)$ %{ENV:directory}%{ENV:three}$1 [L]
This works but i would like to minimize the script

Try it this way for consolidation, if you simplify it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/test/$1/$1.php -f
RewriteRule ^combine/(.*)(?:\.php)$ /test/$1/$1.php [L]

Related

How to rewrite the website URL in .htaccess

is there a way to replace an url with the .htaccess like this:
http://website.com/file.php?watch=video
to this one:
http://website.com/file/watch/video
Tried with this,and haven't worked.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/watch/([\d]+)$ $1.php?watch=$2 [L]
My .htaccess file:
#remove php file extension-e.g. https://example.com/file.php will become
https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/watch/([\w-]+)$ $1.php?watch=$2 [L]
Have it this way:
RewriteEngine on
RewriteBase /test/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /([\w-]+)\.php\?watch=([^\s&]+)\s [NC]
RewriteRule ^ %1/watch/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/watch/([\w-]+)$ $1.php?watch=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

htaccess two endpoints showing same

I have two endpoints on my website /i/ snd /a/. They do show different information, but both the same. Currently, I have the /i/ working like so..
/i/12345 (where 12345 is the itemId).
/a/67890 (where 67890 is the itemId that is active).
If I goto /a/67890, itll show the /i/ UI, not the /a/ UI. Here is my .htaaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.app
RewriteRule ^(.*)$ https://www.hostname.app/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /results/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /a/index.php?id=$1 [NC,L]
What am I doing wrong? It looks like /a/ is being routed to /i/.
Have it this way:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.app$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([ia])/(.*)$ $1/index.php?id=$2 [NC,QSA,L]
RewriteRule ^(results)/(.*)$ $1/index.php?/$2 [L,QSA]

htaccess remove param key not working as intended

I currently have in place a rewrite rule to remove a key on one of the sites.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
I now want to add this rule.
RewriteRule ^(.*)$ /stash/index.php?sid=$1 [NC,L]
However, this does not work.
HOST_NAME/stash/123 will not work.
HOST_NAME/stash?sid=123 will.
The PHP is simple....
echo $_GET['sid'];
Here is my full HTACCESS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.com
RewriteRule ^(.*)$ https://www.hostname.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /stash/index.php?sid=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /results/index.php?/$1 [L]
Any help on this would be great.
UPDATE
There are four main directories on the website.
/results
/i
/stash
/account
I dont care if /results shows ?id=1$store=1...etc
I dont want /i and /stash to show that. I would want those to look like...
/i/123456789 instead of /i?id=123456789
/stash/hgdG54Hj9 instead of /stash?sid=hgdG54Hj9
Looks like you have nice directory based URI structures. In that case you can just have a per directory .htaccess
You may use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(hostname\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Inside stash/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
And similarly inside i/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]

.htaccess rewriterule not working for single page

I have a couple pages:
http://waleyqiao.com/about/
http://waleyqiao.com/index/
http://waleyqiao.com/contact/
http://waleyqiao.com/blog/
http://waleyqiao.com/portfolio/
The real pages would be waleyqiao.com/about.html
htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
For some reason, waleyqiao.com/portfolio/ does not seem to work but waleyqiao.com/portfolio.html does...
It's because you are tacking the .html right on the end of the URI:
/portfolio/.html
which obviously doesn't exist. You need to create a grouping without the trailing slash. Try this instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html
And likewise for the php extension:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.php -f
RewriteRule (.*) /%1.php [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