htaccess rewrite breaks only on a specific URL - apache

htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^dev/(.*)/$ /out.php?q=$1 [L]
RewriteRule ^dev/(.*)/([a-z]+)$ /out.php?q=$1&s=$2 [B,L]
URL request:
http://example.com/dev/100%25%20Booster%20Juice%20(6%20Hours)/supply
For some reason, the above request results in a server error while these:
http://example.com/dev/100%25%20Booster%20Juice%20(6%20Hours)/
http://example.com/dev/Energy%20Juice%20(6%20Hours)/supply
http://example.com/dev/Energy%20Juice%20(6%20Hours)/
work flawlessly and do not generate an error. I have no idea why it's just that one specific URL that doesn't work.

Related

Htaccess rewrite rule redirected but server not processh

I want my server to read this page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
I have tried this .htaccess rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/myapp\.php$
RewriteCond %{QUERY_STRING} ^p=11
RewriteRule ^(.*)$ example.com/myapp/my-technical-effort [QSD,R=301,L]
it redirects to the new URL but the server says "page not found" on this server with 404 error.
Can you please help me to rewrite the rule so the server can serve the page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
My current .htaccess is as follows:
<IfModule mod_headers.c>
Header always append X-Frame-Options ALLOWALL
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>
Anyone can help me please?
Your implementation is the wrong way round. Have a try that way:
RewriteEngine on
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
You need to implement your rewrite rules based on actual imcoming requests. Those should get rewritten internally, so those have to be matched.
This asumes that the actual request you want to rewrite is to /myapp/my-technical-effort (your question is a bit vague in that), and that /myapp.php?p=11 is the internal resource you want to get queried.
UPDATE:
Your comment below suggests that you are actually looking for a solution to redirect the "old" URL to the new one. It is indeed possible to combine that with above rewriting:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=11$
RewriteRule ^/myapp\.php$ /myapp/my-technical-effort [QSD,R=301,L]
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
This will redirect any client that requests the "old" url (https://example.com/myapp.php?p=11) to the "new" url (https://example.com//myapp/my-technical-effort). And it will internally rewrite incoming requests to the "new" url to the actual script able to respond to the requests (/myapp.php).

.htaccess - Internal Server Error

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.

htaccess seo friendly urls, mod rewrite, redirection

I'm currently trying to make SEO friendly URL's for very specific URL's, not all of them. I've been at this for 48 hours with no luck. My goal is to make http://mydomain.com/index.php?p=g&id=1 look like this; http://mydomain.com/pageone/ - so far I have been able to achieve the redirection thusfar, but it is showing a 404 "The requested URL /pageone/ was not found on this server". My question is how to make it redirect to the virtual directory and not throw a 404.
Please note I want to add a rule for each page id, not a rule that changes everything from index.php?p=g&id=, just each specific link.
Below is my htaccess code:
Options +FollowSymLinks
Options -Multiviews
RewriteOptions MaxRedirects=1
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=g&id=1$ [NC]
RewriteRule ^index\.php$ /pageone/? [r=301,nc]
Any help with this would be GREATLY appreciated.
You redirected the index.php to /pageone/ but did not define what /pageone/ will actually show. You should add the following line to your .htaccess:
RewriteRule ^pageone/?$ index.php?p=g&id=1 [L]
and remove your rule. If you want to keep both RewriteRules, then add the following lines right after RewriteEngine On:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

Strange behavior for /index.php/foo

I have a website at http://www.mywebsite.com/~me/ and I want these behaviors:
http://www.mywebsite.com/~me/index.php should redirect to http://www.mywebsite.com/~me/
A URL pointing to something that doesn't exist should redirect to http://www.mywebsite.com/~me/404.php
In looking around for solutions, I found these snippets to include in my httpd.conf file:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /~me/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~me/index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.mywebsite.com/~me/ [R=301,L]
and
ErrorDocument 404 /~me/404.php
These snippets create behavior 1 just fine, and for the most part behavior 2 as well. However, instead of http://www.mywebsite.com/~me/index.php/foo redirecting to the 404 page, it redirects to http://www.mywebsite.com/~me/index.php.
How would I change these snippets to get the behavior I want?

mod_rewrite and relative urls

I'm setting up some simple url rewriting rules using mod_rewrite and a .htacces file, but I've got some problems.
If I set up the .htacces this way:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html /index.php [L]
when I call from the browser this url:
http://localhost/~dave/mySite/index.html
I got a 404 error.
Using this .htacces instead
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html http://localhost/~dave/mySite/index.php [L]
everything works fine and I get the index.php page I'm expecting.
Am I forced to user absolute urls as the target of a rewrite? Is this a Apache configuration problem?
I'm using Max OS X 10.6.2 with its standard Apache installation.
The RewriteBase base directive is setting a base path; you don't need to provide absolute paths in the redirection rules.
The rule pattern is not plain text; it's a regular expression.
If you set a full URL as target you instruct the browser to redirect to that other location; that kind of redirection is not transparent to the user.
To sum up, try this instead:
RewriteRule ^index\.html$ index.php [L]
Try doing
RewriteRule ^(.*)/index.html $1/index.php [L]
That should sort it.