URL Rewriting with .htaccess for HTML pages with query string - .htaccess

I am having some trouble with .htaccess.
I want the URL to be displayed like
https://www.website.com/deals/11015/dealname
Currently I have the following URL.
https://www.website.com/single.html?id=11015&name=dealname
Below is my .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html
RewriteRule . /index.html
RewriteCond %{REQUEST_URI} -f [OR]
RewriteCond %{REQUEST_URI} -d
RewriteRule ^deals/([^/]+)/([^/]+)$ single.html?id=$1&name=$2 [QSA]
When I try to go on https://www.website.com/deals/11015/dealname, it redirects me to index page. Can someone please help?

Related

Redirect in htaccess to folder and https

Using htaccess I want to force http to https and at the same time redirect to www. and to the 'public' folder where my application files are.
The result should be that http://example.com should redirect to https://www.example.com/public/
To redirect to https and www I manage by using:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
To redirect to the 'public' folder I manage by using:
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
Now I need to combine these but my many attempts did not result in a working redirect and I would need some advise on this.
EDIT:
In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
<<
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
The REQUEST_URI server variable starts with a slash, so the condition above will never match. For example:
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) public/$1 [L]
However, assuming you have another .htaccess file in the /public subdirectory I would write this block like the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) public/$1 [L]
This has the added benefit of allowing a public path segment at the start of your URLs (if you wish). eg. The URL /public/foo would internally rewrite to /public/public/foo without creating a rewrite loop.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
You are missing an OR flag on the first condition. For example:
RewriteCond %{HTTPS} off [OR]
:
Without that, it will fail to redirect http://www.example.com/ to HTTPS.
Just to clarify, the canonical redirect should go before the internal rewrite to the public directory.
UPDATE:
EDIT: In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
The substitution string on the last RewriteRule directive is incorrect. You need to remove the slash prefix from /index.php. It should just read index.php. Otherwise it is going to rewrite the request back to the document root (filesystem path) and result in either an endless loop or a 404 (depending on the parent directives). By changing it to index.php (no slash) it is now relative to the /public directory (the directory that contains the .htaccess file), instead of the document root.
You can also simplify the RewriteRule pattern and the NC flag is not required here. So, in summary, the /public/.htaccess file should read:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]

react-router redirect to index.html AND remove www from url in .htaccess

I'm building a small app in ReactJS, so all pages need to serve index.html and the JS handles the url. This works fine. But I'd also like to have .htaccess remove www from the url if it exists. I'm reading through the mod_rewrite documentation and I can't quite figure out how to make it do both.
Here is my code in .htaccess, please advise!
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC]
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
Answered my own question
<IfModule mod_rewrite.c>
RewriteEngine On
# remove www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]
# redirect all to index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L,NC]
</IfModule>

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]

How can I configure htaccess for https-rewriting?

I use https:// for some subdirectories (/login, /admininstration), for this I find some examples - but on the other hand I've to ensure, that all other pages redirect to http://.
Now, I send the user via links to https:// but when they leave the login-Area, they stay in https:// Mode.
I use now RewriteCond in standardconfiguration:
SetEnv APPLICATION_ENVIRONMENT show
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.xxxseitexxx.de$ [NC]
RewriteRule ^(.*)$ http://www.xxxseitexxx.de/$1 [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]
RewriteRule !\.(js|ico|gif|jpg|jpeg|png|css|inc|pdf|csv|zip|rm|mp3|swf)$ index.php
Thanks in advance!
You can use this rule on top of other rules:
# force HTTP for not login or admininstration
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/admininstration|login/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
# force HTTPS for login or admininstration
RewriteCond %{HTTPS} off
RewriteRule ^(admininstration|login)/ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=302]

Resources