Rewriting GET parameters - .htaccess

I'm trying to rewrite $_GET parameters via .htaccess. I have tried doing it myself but no success. Check what I want below:
mydomain.com/system.php?id=748742
to
mydomain.com/system/748742
My /var/www/ directory: prntscr.com/ixpt1x
I have tried using this rule but my server keeps saying 500 Internal Server Error
RewriteRule ^system/(.*) /system.php?id=$1 [NC,L]
Here is my .htaccess file at this current time
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^favicon.ico images/favicon.ico [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(/|)$ $1.php [L]
ErrorDocument 404 404.php
ErrorDocument 403 404.php
ErrorDocument 500 404.php

This is the result of the discussion in the comments to the question:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^favicon.ico images/favicon.ico [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]
RewriteRule ^/?system/(\d+)/?$ /system.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(/|)$ $1.php [L]
ErrorDocument 404 404.php
ErrorDocument 403 404.php
ErrorDocument 500 404.php

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^favicon.ico images/favicon.ico [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]
RewriteRule ^/?system/(\d+)/?$ /system.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(/|)$ $1.php [L]
ErrorDocument 404 404.php
ErrorDocument 403 404.php
ErrorDocument 500 404.php

Related

I need help configuring my htaccess file properly

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

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]

.htaccess mod_rewrite is messing up brackets [ ] in my URL

I changed a few things in my .htaccess file a few days ago and just now noticed that brackets in the URL are now messed up. Instead of:
url.com/admin/page?delete[]=12345&reasons[]=2
the URL shows as:
url.com/admin/page?delete%255B%255D=12345&reasons%255B%255D=2
What could be causing this? Here's my .htaccess file. It's driving me crazy!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L,NE]
RewriteRule ^admin/page/?$ admin/page.php [L,NC]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/profile?user=%1 [L,QSA]
RewriteRule ^([^/]*)-([0-9]+)\.html$ product?name=$1&id=$2 [L,QSA]
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
Have your rules like this:
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/profile?user=%1 [L,QSA,R=302,NE]
RewriteRule ^([^/]+)-([0-9]+)\.html$ product?name=$1&id=$2 [L,QSA]
RewriteRule ^admin/page/?$ admin/page.php [L,NC]
Keep redirect rules before rewrites and use NE flag in each redirect rule.
Make sure to clear browser cache before testing.

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