URL rewrite for / causing 500 internal server error - .htaccess

I have a rewite rule that should redirect the index page (/) to a different page so I can deal with all of my content in there. However adding this second rule caused a 500 internal server error and I am not sure why.
This is in my .htaccess file
Its the 3rd line:
RewriteEngine on
RewriteRule ^search/(.*)/?$ search.php?postcode=$1
RewriteRule ^(.*)$ /turnstile.php?goto=home [L]

You have to exclude the destination you are rewriting to.
RewriteRule ^((?!turnstile.php).*)$ /turnstile.php?goto=home [L]

Related

500 Internal Server Error in my wamp server while trying to Rewrite URL in .htaccess

I'm trying to rewrite my bulky, ugly url to a neat one which is converting "art.php?id=$1" in to "art/1", but I encounter a "500 Internal Server Error" What shall I do? Can you help me pleeeease?
This is the .htaccess code I used:
RewriteEngine On
RewriteRule ^art/([A-Za-z0-9-]+)/?$ art.php?id=$1 [NC,L]
This is the Error I encountered:
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request. Please contact the server
administrator, admin#localhost and inform them of the time the error
occurred, and anything you might have done that may have caused the
error. More information about this error may be available in the
server error log.
Thanks!
Supposed everything is in the gallery folder, which itself is located in the www root directory. Then put this .htaccess file into the gallery folder as well
RewriteEngine on
RewriteBase /gallery/
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# serve real content
RewriteRule ^art/([A-Za-z0-9-]+)/?$ art.php?id=$1 [NC,L]
# make pretty URL
RewriteCond %{QUERY_STRING} id=(.+)
RewriteRule ^art.php$ art/%1? [R,NC,L]
Depending on the server you're on, you might want to set the base for rewriting
RewriteEngine On
RewriteBase /art/
RewriteRule ^([A-Za-z0-9-]+)/?$ art.php?id=$1 [NC,L]
that should work just fine

Rewrite rules error

I want to rewrite a simple url, but without generating google errors
This code works :
RewriteEngine on
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L]
but i want to add R=301 flag for SEO
When i add [R=301,L] :
The requested URL /var/www/mysite/index.php was not found on this server.
I know that R=301 flag must be used with http://
but when i try the url is not rewritting
Apache tries to guess whether a path is a URI-path or a file-path, and it's guessing wrong. When you are internally rewriting, a file-path is perfectly fine, because it's all internal to the server. But when you are redirecting, apache incorrectly guesses that your target (the index.php?eID= ) is a file-path and it gets flagged to be handled by mod_alias as a redirect. By the time the redirect happens, it's malformed as a file-path instead of URI path. That's why you're getting the /var/www/mysite/ bit when you redirect.
Either add a RewriteBase to provide a URI base for relative URIs, or make your target an absolute URI:
RewriteEngine On
RewriteBase /
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [R=301,L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L,R=301]
or
RewriteEngine on
RewriteRule lieu/([0-9]+).* /index.php?com=location&lID=$1 [L,R=301]
RewriteRule evenement/([0-9]+).* /index.php?eID=$1 [L,R=301]

htaccess redirect to different page on 403 Forbidden Error

Can't find this for the life of me. We have the indexes protected so if someone goes to CDNdomain.com/1/ they won't see anything unless they do CDNdomain.com/1/something.jpg. When they visit /1/ they are presented with a 403 Forbidden error. What I would like to do is when they are shown a 403 Forbidden error to instead redirect them to ourRealdomain.com.
Any ideas?
You can try something like this in the htaccess file in the document root of CDNdomain.com:
RewriteEngine On
# make sure this is the right host
RewriteCond %{HTTP_HOST} cdndomain.com$ [NC]
# make sure this is a request for an existing directory
RewriteCond %{REQUEST_FILENAME} -d
# redirect
RewriteRule ^(.*)$ http://ourRealdomain.com/ [L,R=301]
Note that this will also redirect /: http://CDNdomain.com/ to http://ourRealdomain.com/. If you want to avoid that, change the regex match from ^(.*)$ to ^(.+)$. If you want to put these rules in the vhost config for the CDNdomain.com, add a slash after the ^: ^/(.+)$.

.htaccess domain redirection to folder but preventing direct access with 404

I have a shared hosting in which I want to host several websites. To this end, and to keep things clear, I have redirected my primary domain (i.e. www.mydomain.com) to a folder (i.e. /mydomain.com/). However, I want to prevent direct access to this folder (www.mydomain.com/mydomain.com/) from the URL with a 404 error (not found).
This is the .htaccess in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain.com/0.1/$ [NC]
RewriteRule ^(.*)$ /mydomain.com/0.1/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain.com/0.1/index.php [L]
RewriteCond %{THE_REQUEST}% ^[A-Z]{3,9}\ /mydomain\.com/ [NC]
RewriteRule . - [R=404,L]
And this is the .htaccess in /mydomain/ with a bunch of rules for nice and tidy URLs.
RewriteEngine On
RewriteRule ^about/(.+) index.php?a=about&b=$1
RewriteRule ^services/(.+) index.php?a=services&b=$1
Right now this is the result:
Accessing www.mydomain.com shows the content in /mydomain/ as hoped. However, www.mydomain.com/mydomain/ is also displayed, ignoring the 404 rule, and creating another folder -whatever the name- without an htaccess DOES throw the 404.
I've been dealing with this problem for 5 days now and tried everything. At this point I don't know if the error comes from the root .htaccess or the folder's.
Thanks in advance.
PS: Just to be clear, I have no control over the httpd.conf file.
Notes:
I forgot saying that there is an additional folder inside /mydomain.com/ called /0.1/ for version control.
If a include "RewriteOptions Inherit" after "RewriteEngine On" in the /mydomain.com/0.1/ htaccess, I get a 500 internal server error.
Deleting the /mydomain.com/0.1/ htaccess file altogether will produce the desired 404 error for www.mydomain.com/mydomain.com/0.1/ and produce a 505 in www.mydomain.com
The R flag in rewrite rule should be something between 301 to 399 , so it can not be 404!!!
your rule should be like below:
RewriteCond %{THE_REQUEST} ^/mydomain\.com/? [NC]
RewriteRule (.*) throw_404.php [L]
create throw_404.php and to send 404 status code! and add L flag to your first rule for avoiding conflicting between that rule and mine!

HtAccess Rewrite Needed

My host will not allow me to change the default folder of my primary domain. I have managed to Rewrite http://www.mysite.com to the real folder
public_html/mysite.com/www/
with the following code:
RewriteEngine On
RewriteRule ^$ /mysite.com/www/ [R=301,L]
This does successfully load my domain from the subfolder, but the url becomes:
http://mysite.com/mysite.com/www/
How can I continue loading requests from http://mysite.com/index.html in the correct folder shown above, without showing it in the client-side url?
Try this one:
RewriteEngine On
RewriteRule ^mysite.com/www/(.*) - [L]
RewriteRule ^(.*)$ mysite.com/www/$1 [L]
UPD:
The line with the dash is required because after the redirect at line 3 Apache reads the .htaccess once again to process the redirected URL. The rule prevents infinite loop.
Try removing the R=301.

Resources