.htaccess - Internal Server Error - apache

I'm trying to make the following rewrite:
http://example.com/folder1/folder3/index.html
to:
http://example.com/folder1/folder2/folder3/index.html
for that I've tried the following .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^folder1/folder2/.*$
RewriteRule ^folder1/(.*)$ /folder1/folder2/$1 [L]
But I get: Internal Server Error
In the other hand, if I do an experiment (just for testing) like:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^folder1/folder2/.*$
RewriteRule ^folder1/(.*)$ /kkk/folder2/$1 [L]
Then the rewrite:
http://example.com/folder1/folder3/index.html
to:
http://example.com/kkk/folder2/folder3/index.html
works. But I need the first rewrite.
Any idea on how to solve this?
[edited word "redirection" -> word "rewrite"]
[edited to add the error.log content]
On the following jsbin you have the error.log content:
http://jsbin.com/fajohugase/1/edit?output

The URI in REQUEST_URI variable contains the leading / as it is the path part of your URL. So, when you're using the !^folder1 condition, it fails to match, causing an infinite loop.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folder1/folder2/
RewriteRule ^folder1/(.*)$ /folder1/folder2/$1 [L]
As to your redirection, you are not actually redirecting the client; but merely internally rewriting the URL to include /folder2 in it. For a redirect, you'll need the R flag.

Related

Rewriting url conditionally and making everything lowercase

So, I've urls like this: https://foobar.com/blog?category=Foo+Bar , where the actual get parameter value may or may not have plus( + ) character in it.
What I'm trying to do is first make parameter value lowercase, in all cases, and then, if parameter value contains plus character( + ), replace it with hyphen.
In the end, I'm trying to get https://foobar.com/blog?category=Foo+Bar rewritten to https://foobar.com/category/foo-bar
This is what I got so far:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/blog
RewriteCond %{QUERY_STRING} category=([^/]+)+([^/]+)$
RewriteRule (.*) /category/%1-%2? [R=301,L]
Now, that's somewhat close, but it actually rewrites https://foobar.com/blog?category=Foo+Bar to https://foobar.com/category/Foo+Ba-r.
Any help would be much appreciated.
Please add RewriteMap lc int:tolower at last of your htaccess file or in your <VirtualHost> section. Could you please try following then.
RewriteEngine On
RewriteCond https on
RewriteCond %{REQUEST_URI} ^/blog$ [NC]
RewriteCond %{QUERY_STRING} ^([^=]*)=([^+]*)\+(.*)$
RewriteRule ^(.*)$ /%1/${lc:%2}-${lc:%3} [QSD,R=301,L]
Testing with curl command(with http but rule above takes care of https part):
curl -IL "http://localhost:80/blog?category=Foo+Bar"
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
Location: http://localhost/category/foo-bar

Rewrite part of url with .htaccess

I need to rewrite URL and save top level domain and query.
I tried to use these rules
RewriteEngine On
RewriteBase /
RewriteRule ^domain(.*)$ http://newdomain$1 [R=302,NE,L]
Using this testing tool I found that it works if domain.com/query?param=value is used as a request URL. But if I try to use http://domain.com/query?param=value[ it doesn't work.
Basically I don't care what protocol is (http or https), I just need to replace first occurrence of domain string and rewrite it with newdomain saving all other parts of a request URL.
As it turned out in your comment to the first answer I gave you are actually trying to replace only part of the hostname of the incoming request but keep path and query string. That was not clear to me from your question, sorry.
You have to use an additional RewriteCond for this, since as said before you caanot access the hostname at all inside a RewriteRule. So I guess the following goes into the direction of what you are actually looking for:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ http://newdomain.%1/$1 [R=301,L,QSA]
You may want to try this modification to preserve the original request scheme too:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://newdomain.%1/$1 [R=301,L,QSA]

Apache htaccess sending URLs in a URL

I'm trying to send a URL in a URL, and there's ModRewrite in the middle.
Here's the Rewrite Rule. It's used to search for a user's profile.
RewriteEngine On
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
And this is the URL
http://www.example.com/query/profile/http://www.facebook.com/example
No matter how I do the rewrite rule, I keep getting NetworkError: 500 Internal Server Error or NetworkError: 404 Not Found if it's a URL I put there.
How can I do this right? I'm on Windows 7 Home Premium. Am I missing something?
The URL above is passed using ajax.
Edit:
#htaccess in site1 folder
RewriteRule ^resources/?(.*)/?$ /control/$1 [NC,L]
#htaccess in control folder
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
next folder is `search` which has `index.php`
URL used
http://www.example.com/resources/query/profile/http://www.facebook.com/example
EDIT:
I redid the RewriteRule like below, and don't get any error messages now, but Apache seems to be taking off one / from the url after the http://. So I get only http:/www.facebook.com/example. Any help with that will be appreciated.
RewriteRule ^(query)/(profile)/([a-z0-9\/:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]`
You need to capture URL from RewriteCond otherwise Apache will trim multiple // into a single one.
Change this rule in root .htaccess:
RewriteCond %{REQUEST_URI} /resources/(.+)$ [NC]
RewriteRule ^ /control/%1 [L]
Have this in /control/.htaccess file:
RewriteEngine On
RewriteBase /control/
RewriteCond %{REQUEST_URI} /(query)/(profile)/(.+)$ [NC]
RewriteRule ^ /resources/search/index.php?scope=%2&query=%3 [QSA,L]

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1

direct all webpage requests with .index.html to /

I want to direct all requests for any URL that ends with index.html to /. I have one domain on the server.
Example:
If someone wants "www.thissite.com/index.html--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/anyword/index.html"--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/folderdoesntexistonthissite/index.html"--it is directed to www.thissite.com/.
What is the .htaccess code that would enable this? (Both the rewritecondition and rewriterule)
This doesn't quite do the job:
RewriteCond %{THE_REQUEST} index\.html [NC]
RewriteRule index\.html$ http://www.thissite.com/$1 [R=301.L]
You could try this (without RewriteCond):
RewriteRule /index\.html$ http://www.thissite.com/ [R=301,NC,L]
Maybe the Error was the Period in [R=301.L].
You will need to use %{REQUEST_URI} variable to match in RewriteCond otherwise Apache strips out starting / in RewriteRule. Use below code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index.html$ [NC]
RewriteRule . / [R=301,L]