Simple Modrewrite rule - mod-rewrite

i want to redirect only this url
http://domain.com/?eID=dd_googlesitemap
to
http://domain.com/test.html
my rule is
RewriteRule http://domain.com/?eID=dd_googlesitemap http://domain.com/test.html [R=301,L]
but it doesnt work. I dont get it. Any ideas whats wrong?
. . . . . . . . . . . . . . . . . . . . . . . . . . .

The problem you're getting is that query strings and domains aren't matched by a RewriteRule. Instead you would need to specify these as conditions prior to the rule, using RewriteCond:
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteCond %{QUERY_STRING} ^eID=dd_googlesitemap$
RewriteRule ^$ http://domain.com/test.html? [R=301,L]
If you don't specify any [flags] on the RewriteCond's, then they are AND'd, so here the domain part of the requested URL (the HTTP_HOST) must be "domain.com"... "www.domain.com" will not be matched. Also, if any other options are exist in the query string, it won't match.
Finally, we rewrite a completely empty request (no additional paths, etc), to the new URL. Adding ? to the end of the URL stops the query string being added.

Related

Laravel trailing slash connects to GET instead of POST

I have a POST route setup called user/connect which points to the relevant controller function ( which expects post varialbles), but adding a trailing slash e.g. user/connect/ redirects it to GET thus returning "MethodNotAllowedHttpException"
I am unable to remove the forward slash ( as the consuming app is sending it and i have no control ) . Any pointers on how i could get the route with the trailing slash to also point to my POST route ?
You should use ‘php artisan list:route’ to find a conflicting route. Laravel will not alter the HTTP method. The app could also be wrong so some investigation is needed there as well.
Fixed using user3532758 comment, seems that if i remove the following from the public/.htaccess it fixes it, but i am not sure if it opens up another security hole so checking that as well
removing the following fixes it:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

htaccess rewrite all image requests from a non-existent folder to folder-name.jpg

Here's what I am trying to do, for example . . .
http://www.website.com/images/folder-with-a-crazy-name/any-image-at-all.jpg
would rewrite to . . .
http://www.website.com/images/folder-with-a-crazy-name.jpg
Does this make sense?
Any help would be appreciated.
To rewrite all jpeg image files in a folder to that folder with .jpg appended
RewriteRule ^images/(.+?)/[^/]+?\.jpg$ /images/$1.jpg [L]
Just paste this into your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^images/([a-z0-9-_]+)/any-image-at-all.jpg /images/$1.jpg
Or if you want even the other image formats to be rewritten, then just replace the third and the last line on the code above with this rule:
RewriteRule ^images/([a-z0-9-_]+)/any-image-at-all.([a-z0-9]{3}|[a-z0-9]{4}) /images/$1.$2
If the the folder images is also a variable, then try one of the following rules below:
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/any-image-at-all.jpg /$1/$2.jpg
If the file extension is a variable, then try this:
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/any-image-at-all.([a-z0-9]{3}|[a-z0-9]{4}) /$1/$2.$3

rewriting URLs from .json to .php

I want http://server/path/app.json?a=foo&b=bar to map to http://server/path/foo.php?a=foo&b=bar using mod_rewrite. I have the following incantation in my .htaccess which doesn't give any joy
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Suggestions?
Update: Adding rewrite.log and error.log output (comments don't allow formatting)
I get the following in the rewrite.log
strip per-dir prefix: /Users/user/Sites/dir/app.json -> app.json
applying pattern '^([^.?]+).json(.*)$' to uri 'app.json'
rewrite 'app.json' -> 'app.php'
add per-dir prefix: app.php -> /Users/user/Sites/dir/app.php
internal redirect with /Users/user/Sites/dir/app.php [INTERNAL REDIRECT]
and the apache server log says
The requested URL /Users/user/Sites/dir/app.php was not found on this server.
If I read your question correctly you want:
http://server/path/app.json?a=foo&b=bar
Going to:
http://server/path/foo.php?a=foo&b=bar
Sowhen you capture (app).json $1 is app and $2 is your second parenthesis, it's ... nothing (the part between json and the ?). As everything after the question mark is the QUERY STRING and cannot be captured here. Your rewriteRule is working on the requested file, not on the QUERY STRING. So you didn't captured foo anywhere. For the QUERY_STRING you could use the [QSA] flag on the rewriteRule, that would simply append a=foo&b=bar after your rewrite.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Here you tell apache to reuse $1 (the filename without .json), so app.json will get redirected to app.php, not foo.php.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php [L,QSA]
Will redirect app.json?a=b&z=r to app.php?a=b&z=r.
Now if you really need to capture foo as the first QUERY_STRING parameter the rule will become harder. But you could do it like that (here instead of the first parameter I detect the parameter 'a=' and capture his value in %4):
RewriteCond %{QUERY_STRING} (.*)(^|&|%26|%20)a(=|%3D)([^&]+)(.*)$
RewriteRule ^([^.?]+).json$ %4.php? [L,QSA]

Re-write userprofile URL (remove.html)

Any Ideas how to change remove .html in this mod-rewrite script
Doesnt work if I remove ".html"
RewriteRule ^([^/]*)\.html$ /userprofile.php?member_id=$1 [L]
Works as
http://site.com/12.html
but wants to have it as
http://site.com/12
Thank you
To make the .html optional, put it in a group and use the ? quantifier:
RewriteRule ^([^/]*)(\.html)?$ /userprofile.php?member_id=$1 [L]
But as this pattern will now also match any single path segment, you should make it more specific to only match your specific URL path pattern. In this case \d+ instead of [^/]* would be a better choice:
RewriteRule ^(\d+)(\.html)?$ /userprofile.php?member_id=$1 [L]
MODIFIED:
I have tested the following and they work on a CentOS server running Apache.
For directory rewriting:
RewriteRule ^/test/(.*)$ http://www.google.com [L,R]
That will redirect http://www.site.com/test to http://www.google.com.
For all files in a directory this will work:
RewriteRule ^/test/([^/]*)(.*)$ http://www.google.com [L,R]
That will redirect www.site.com/test/12.html or www.site.com/test/298.aspx or any other file in the "test" directory to www.google.com.
So this may be more what you are looking for:
RewriteRule ^/12/([^/]*)(.*)$ /userprofile.php?member_id=$1 [L,R]
ORIGINAL POST:
I believe this is what you are looking for:
RewriteRule ^([^/]*)(.*)$ /userprofile.php?member_id=$1 [L]
That is if you are trying to do the rewrite for files...
It will be slightly different if you want to do the rewrite against a directory.
You need to remove the backslash as well ("\.html").

mod_rewrite issues

I have used the mod_rewrite module but was not able to redirect to the target page - I am getting an error:
The requested URL /old.html was not found on this server.
Rewrite rules as follows:
RewriteEngine On
RewriteRule ^/IN/index.html$ /IN/index.iface [L]
You need to request a URL with a path that’s matched by the pattern of your RewriteRule directive. So in your case obviously /IN/index.html (where the . can actually be any character, as it’s not escaped).

Resources