I need help configuring my htaccess file properly - .htaccess

so I got this script from codester and I wasn't able to install it cause I keep getting errors like ".htaccess misconfigured" can anyone detect the error for me please, here is the .htaccess code
RewriteEngine on
RewwriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule page/(.*)$ page.php?slug=$1 [L]
RewriteRule py$ _py.php [L]
RewriteRule payout$ pyt.php [L]
RewriteRule contact$ ct.php [L]
Options -Indexes
ErrorDocument 404 /404.php
Help

There is a spelling mistake in your second line, use
RewriteBase /
instead of RewwriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule page/(.*)$ page.php?slug=$1 [L]
RewriteRule py$ _py.php [L]
RewriteRule payout$ pyt.php [L]
RewriteRule contact$ ct.php [L]
Options -Indexes
ErrorDocument 404 /404.php

Related

Removing the .php file extension from the URL Request

I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.
I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.
Here's what I got so far,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [R]
</IfModule>
I would like my domains to look like
https://www.example.com/about
Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.
Have it this way:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to clear your browser cache before testing this change.
Put in .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]
Redirect 301 /index /
remove .php from the link :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

rewrite sub folder url query string

I am new to web development.
Below Htaccess code works fine in root directory files. While We try to rewrite the files in sub folder /test/downloadh.php. when i was changed below code will we can't get the root files links are notworking.
RewriteEngine on
RewriteBase /test/
Would you please suggest us, how to get the url
http://example.com/test/downloadh/the-house-on-the-borderland
to
http://example.com/test/downloadh.php?id=the-house-on-the-borderland
Working Code:-
# disable directory browsing
Options -Indexes
AddDefaultCharset UTF-8
FileETag MTime Size
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+album\.php\?Id=([^\s&]+) [NC]
RewriteRule ^ album/%1? [R=301,L]
RewriteRule ^album/([^/]+)/?$ album.php?Id=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([^\s&]+) [NC]
RewriteRule ^ download/%1? [R=301,L]
RewriteRule ^download/([^/]+)/?$ download.php?id=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+downloada\.php\?linkb=([^\s&]+) [NC]
RewriteRule ^ downloada/%1? [R=301,L]
RewriteRule ^downloada/([^/]+)/?$ downloada.php?linkb=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:downloadb\.php)?\?mlink=([^\s]+)&linkb=([^\s]+)? [NC]
RewriteRule ^ downloadb/%1%2? [R=301,L]
RewriteRule ^downloadb/([^/]+)/([^/]+)/?$ downloadb.php?mlink=$1&linkb=$2 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:songs\.php)?\?id=([^\s]+)&linkb=([^\s]+)? [NC]
RewriteRule ^ songs/%1/%2? [R=301]
RewriteRule ^songs/([^/]+)/([^/]+)/?$ songs.php?id=$1&linkb=$2 [L,QSA]
# CUSTOM ERRORPAGE
ErrorDocument 401 /error/test.php
ErrorDocument 403 /error/test.php
ErrorDocument 404 /error/test.php
ErrorDocument 500 /error/test.php
Error Code:-
RewriteCond %{THE_REQUEST} /downloadh(?:\.php)?\?id=([^\s&]+)\s [NC]
RewriteRule ^ downloadh/%1/? [R=302,L,NE]
RewriteRule ^downloadh/([^/]+)/?$ downloadh.php?id=$1 [NC,L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test/downloadh.php\?id=([^\s&]+) [NC]
RewriteRule ^test/downloadh\.php /%1? [R=301,L,NE]
RewriteRule ^downloadh/([^/]+)/?$ /test/downloadh.php?id=$1 [L,QSA]
Try this code :
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(test/downloadh)\.php\?id=(.+)\sHTTP.*$ [NC]
RewriteRule ^ %1/%2? [L,R=301]
RewriteRule ^test/downloadh/([^/]+)/?$ test/downloadh.php?id=$1 [L]

Why is my .htaccess with mod_rewrite giving me a 500 error?

My .htaccess looks like this:
<FilesMatch ".(eot|ttf|otf|woff|svg)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L,NE]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/page?user=%1 [L,QSA,R=302,NE]
***THIS IS WHERE I INSERT THE NEW RULE***
RewriteRule ^([^/]*)-([0-9]+)\.html$ product?name=$1&product_id=$2 [L,QSA]
So far so good. It all works. When someone goes to scoobydoo.domain.com for example they are redirected to domain.com/page?user=scoobydoo. Now I also want to mask the page so that when someone goes to www.domain.com/scoobydoo they are really shown domain.com/page?user=scoobydoo, so I add this:
RewriteRule ^([^/]*)$ /page?user=$1 [L]
When I add this to the 2nd last line however, it doesn't work.
What's wrong here?
Your rule is ok, but you need to exclude your real files and dirs from it :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /page?user=$1 [L,QSA]

Request exceed the limit of 10 internal redirects due to probable configuration error

My htaccess is as below:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# restrict .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^\ ]+\.php($|\ )
RewriteRule \.php$ / [F,L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
RewriteRule ^good-([^-]*)-([^-]*)\.html$ /goodie?
sd=$1&sd=$2 [L]
RewriteRule ^goodies-([^-]*)-([^-]*)$ /goodie_new?
sd=$1&ds=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /goodie_new?
ds=$1&sd=$2&de=$3 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail_fm?prmn=$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
When i access blog rewrite URL i.e. http://example.com/blog/title-of-blog then i am getting 500 Internal Server Error. I tried adding RewriteCond %{ENV:REDIRECT_STATUS} ^$ but still 500 error is not disappearing.
Please let me know errors and if there is any wrong rules in myhtaccess.
Have it this way:
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} /index\.php
RewriteRule ^index\.php$ / [L,R=301]
# skip files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# restrict .php
RewriteCond %{THE_REQUEST} \.php($|\ )
RewriteRule \.php$ / [F,L]
# remove .html
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
RewriteRule ^goodies-([^-]*)-([^-]*)$ goodie_new?sd=$1&ds=$2 [L,QSA]
RewriteRule ^good-([^-]*)-([^-]*)\.html$ goodie?sd=$1&sd=$2 [L,QSA]
RewriteRule ^blog/([^/.]+)/?$ blogdetail_fm?prmn=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ goodie_new?ds=$1&sd=$2&de=$3 [L,QSA]
# remove .php from URL
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ cityres?cityf=$1 [L,QSA]

.htaccess redirect not found page to a specific page

Here's my code
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^siteuri_partenere$ siteuri_partenere.php [QSA,L]
RewriteRule ^politica_de_confidentialitate$ politica_de_confidentialitate.php [QSA,L]
RewriteRule ^blog - [L,NC]
RewriteRule ^([^/.]+)/?$ game.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error [L,R=302]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
So, if i change this line
RewriteRule . /error [L,R=302]
to
RewriteRule . /page_not_found [L,R=302]
and then i test www.domain.com/1q2w3e, is still redirect to /error and not to /page_not_found so i'm very confusing where is the problem?
Most likely you have some ErrorDocument 404 directive that is overriding your rewrite rules. Place this code in your root .htaccess:
Options +FollowSymLinks -MultiViews
ErrorDocument 404 default
RewriteEngine On
RewriteRule ^blog - [L,NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^error$ error.php [L]
RewriteRule ^siteuri_partenere$ siteuri_partenere.php [L]
RewriteRule ^politica_de_confidentialitate$ politica_de_confidentialitate.php [L]
RewriteRule ^([^/.]+?)/?$ game.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /page_not_found [L,R=302]

Resources