URL rewrite 500 Internal Server Error - php

Original url : mydomain/index.php?lang=english
I want url : mydomain/english
My .htaccess file below :
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?lang=$1 [L]
But I get 500 internal server error. How can I solved this problem ?
Thanks.

What about this:
RewriteRule ^([^/]+)$ ?lang=$1 [L] # and maybe QSA too

The rewrite engine loops until the URI stops changing. Try adding some conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?lang=$1 [L]

Related

.htaccess return 500 error fot rewritten URL

I try to rewrite this URL
http://domain.com/page?LINK=member-name
to be
http://domain.com/page/member-name
The rule I've tried is:
RewriteEngine On
RewriteRule ^page/([^/]*)$ /page?LINK=$1 [L]
I call the request like this:
John Smith
Try this, i am assuming page has .php extension.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]*)$ page.php?LINK=$1 [L]

htaccess 404 redirect does not working

I have a .htaccess file to manage rewrite_rules on my website. I also set a 404 redirect command to navigate visitors to a specefied page if they enter a wrong URL. But this redirect does not working and I get 500 Internal Server Error if visitor request an invalid URL. Below is some part of my codes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?lang=$1&page=home [NC,QSA,L]
ErrorDocument 404 http://www.mywebsite.com/404/
What is missing in my code that causes this problem?
The culprit, by seeing all of your .htaccess rules is:
RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
Giving your sample URL http://www.domain.com/en/news/1/something/test/, when a URL does not match with defined rules it hits this last rewrite rule and it falls into an infinite loop:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/(p[0-9]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^(.*)$' to uri 'en/news/1/something/test/'
...
Then we have a recursion start:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
Although a URL like http://www.domain.com/en/news/1/wrong is not found, it's caught by a defined rule (first rule) and we don't encounter any recursion. So this internal error is not thrown on all not found pages but those that are not caught by a rule.
I don't see it right, just remove it.
you should put
<IfModule mod_rewrite.c>
RewriteEngine On
in the start and
</IfModule>
in the end of your .htaccess code
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
You can use an EroorDocument directive to forword non existent requests to a specific destination
Test this in an empty .htaccess file
ErrorDocument 404 /page.php
Check below code, I have write with various scenario.
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
It seems your mentioned URL after ErrorDocument is not valid or is not reachable.
So, what happening is that when it gets a 404 it redirects to your URL, and it gets a 404 again.
Forms a recursive loop.
Getting my point. ?
Try with a simple static html page.
I hope this shall work.
Custom 404 will never work if you add domain like http://www.mywebsite.com/404/ (it's not coding karma), when you type url like this it will ended up in 500 error. It means it executed 404 but redirected user to 500 error due to wrongly configured htaccess.
When error comes in series of 4XX are knows as client side errors and 5XX known as server side errors. For detailed info, 2XX known as Success & 3XX known as Redirection.
So here is quick try (i assume you have 404 html file)
ErrorDocument 404 /404.html
instead of
ErrorDocument 404 http://www.mywebsite.com/404/
I also have tested this code at my side and it worked. Please try out
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
ErrorDocument 404 /404.html
Try this one:
ErrorDocument 404 /404/
Try adding before rewriterules
RewriteEngine On

How to rewrite an url via htaccess

I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]

.htaccess rewrite with codeigniter

I've got a codeigniter setup where all URLs redirect to index.php, like below.
I need /api/index.php to go to /api, but NOT through a redirect.
My code below matches api/index.php but it is still not working.
/index.php/api works fine, and goes to the correct URL, however /api/index.php just goes to a 404 error page... Any suggestions? Am I being silly?
RewriteEngine on
RewriteBase /
RewriteRule ^api/index.php$ /index.php/api [L]
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ /index.php/$1 [L]
Probably because it is looking for a file named /api/index.php
Add these Rewrite Conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
//Your rewrite rule here
Try this:
RewriteEngine on
RewriteCond $1 !^(index\.php|img|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Error in url rewrite if i remove .html from url

I am having some issues with url rewriting
I am using this as my code for rewrite in htaccess
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /pages.php?id=$1 [L]
this was my url
studentsassignmenthelp.com/pages.php?id=Marketing-Assignment-Help
after using the url rewrite i am getting this
studentsassignmenthelp.com/Marketing-Assignment-Help.html
but i need it without the html like the below one
studentsassignmenthelp.com/Marketing-Assignment-Help
if i use the below code to remove the .html than it shows error 500 server...
RewriteRule ^([^/]*)$ /pages.php?id=$1 [L]
I saw some example but after using them it shows 404 or 500 error
Try this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !pages.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages.php?id=$1 [QSA,L]
try this:
RewriteEngine On
RewriteRule ^([^/]*)$ /pages.php?id=$1 [L]
redirectMatch 301 ^(.*).html $1
This is quite usefull to remove any url extension and avoid broken links.

Categories