RewriteRule creating 500 Internal Server Error bad flag delimiters - mod-rewrite

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_URI} /login/
RewriteRule /login/(.*)$ /php/login/$1

I am not a htaccess expert but I think the error is in your last line.
Try replacing this line of htaccess
RewriteRule /login /php/login $1.php
with this
RewriteRule /login/ /php/login.php
I am guessing that you want to rewrite from /login/ to /php/login.php

Related

Redirect based on the beginning of the path

I'm building a Laravel project. The project itself has a "legacy" version with a whole different structure. It's logged that some users are trying to access a file that is not found in the new environment.
For a quick "fix", we want to redirect paths like
/media/22044/blablabla.pdf to /en/press
The quick solution is to use
Redirect 301 /media/22044/blabla.pdf /en/press
But we want the path behind /media/ to be dynamic.
I'm new with .htaccess stuff.
Is there a way to do it?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
# this one works, but not dynamic
#Redirect 301 /media/2336/blabla.pdf /en/press
# failed experiments
#Redirect 301 (\/media)(.*)$ /en/press
#Redirect 301 ^/media/(.*)$ /en/press
#RewriteRule ^/media/(.*) /en/press [R=301,NC,L]
Redirect 301 /media/(.*) /en/press
</IfModule>
You need to use a mod_rewrite RewriteRule directive (not Redirect) before your existing rewrite.
For example:
RewriteEngine on
# Redirect "/media/<anything>" to "/en/press"
RewriteRule ^media/ /en/press [R=301,L]
# Rewrite all requests to the "/public" subdirectory
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
The Redirect directive uses simple prefix-matching, not a regex. But is also processed later in the request.
And note that the URL-path matched by the RewriteRule pattern does not start with a slash, unlike the Redirect directive.
With your shown samples and attempts please try following .htaccess rule file. Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public [NC]
RewriteRule ^(.*)/?$ public/$1 [L,NC]
###new redirect here...
RewriteCond %{THE_REQUEST} \s/.*/media/22044/blablabla\.pdf\s [NC]
RewriteRule ^ /en/press? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en/press)/?$ media/22044/blablabla\.pdf [NC,L]
</IfModule>

.htaccess - check if part of URL is missing - 301 redirect for ajax URLS

I have made a copy of a wordpress site and set it up on localhost (WAMP), everything works well apart from all of the AJAX calls as the URLs have not been updated.
I need the urls to be in the following format: http://localhost/mysite/dashboard/ but instead they have the /mysite/ part missing ie. http://localhost/dashboard/
I assumed this could be resolved by doing a 301 redirect in the same way a none www domain is redirected to a www. I have tried the below but it doesn't do anything:
RewriteCond %{HTTP_HOST} !^/mysite/\.
RewriteRule ^(.*)$ %{HTTP_HOST}/mysite/$1 [R=301,L]
---------------------HTACCESS FILE----------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/index.php [L]
RewriteRule !^mysite/ /mysite/%{REQUEST_URI} [R=301,L,NC]
</IfModule>
# END WordPress
That rule should be:
RewriteRule !^mysite/ /mysite%{REQUEST_URI} [R=301,L,NC]
%{HTTP_HOST} variable only matches domain name in the request not the REQUEST_URI.

Redirect AJAX query by .htaccess

I have a .htaccess file:
RewriteEngine On
DirectoryIndex control.php
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) controller.php?do=$1 [L]
This redirects all queries and sets up clean URLs. But when I use ajax it redirects that too. Is it possible to filter ajax queries and redirect to
controller-ajax.php?do=$1 [L]
instead?
I was trying to catch it in controller.php, but X_REQUESTED_WITH doesn't exist. The best thing then would be to redirect to another script all ajax requests, and not make an additional script check.
When you call AJAX, add a GET variable to the query (i.e. add ?ajax=true to the end of the query). Then have this in your .htaccess:
RewriteEngine On
DirectoryIndex control.php
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f {OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ajax.true
RewriteRule (.+) controller-ajax.php?do=$1 [L]
RewriteRule (.+) controller.php?do=$1 [L]

Redirect missing final slash

Here's my redirect rule:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^somedir\/?(.*)$ "http\:\/\/mydomain\.com\/newdir\/somedir\/$1" [R=301,L]
When trying to reach
mydomain.com/somedir/Test.pdf
I am redirected to
www.mydomain.com/newdir/somedirTest.pdf (no slash before Test.pdf)
instead of
www.mydomain.com/newdir/somedir/Test.pdf (slash present)
What am I missing?
edit:
Here is my entire .httaccess
RewriteEngine on
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^somedir/?(.*)$ "http://mydomain.com/newdir/somedir/$1" [R=301,L]
I have noticed that redirection works okay if I write
www.mydomain.com/somedir/Test.pdf
but redirects without a final slash with
mydomain.com/somedir/Test.pdf
The code seems right. Although I suggest you to change the RewriteRule to:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule ^somedir/?(.*)$ http://mydomain.com/newdir/somedir/$1 [R=301,L]
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
There is no need of the backslashes.

Need mod_rewrite condition and rule to replace ".com" with ".org" on 1 virtual host

I have a series of mod_rewrite directives (shown below as Code 1) that run every server request through a custom PHP app.
Code 1
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Before I get to the last step in the mod_rewrites, I need to change any request to mydomain.com to mydomain.org. Code 2 below shows what I am thinking but it does not work. The request gives me 500 Internal Server error.
Code 2
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^ http://mydomain.org%{REQUEST_URI}
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Can someone offer a suggestion? Thanks
I asume your site is reachable by two domains, but you want all request to your site redirected to one of them. Then the correct code is:
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
You should place that code directly after the redirect line.
The second line makes sure that there is no redirect if no Host is given (to prevent a redirect loop in that case).
The [R=permanent,L] in the third line makes it a permanent redirect and prevents any further rule processing. The other rules will be processed once the redirect has taken place.
The full file would then be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Resources