Apache htaccess sending URLs in a URL - apache

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]

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

Redirecting All Requests to Index.html

I would like to redirect all requests made to my website to index.html if the request aren't made from AJAX. Apart from just redirecting, I would like to append the request URI as a get parameter to index.html, for example if someone visits http://example.com/something.html, the person should be redirected to http://example.com/?origin=something.html. To achieve this, I have the following code in a .htaccess file
RewriteEngine On
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_URI} !^/(index.html)?$ [NC]
RewriteRule ^/.*.html$ /index.html?origin=%{REQUEST_URI} [R=301,L,NC]
However, it does not redirect any requests as expected. I'm on Apache/2.4.33, Ubuntu 16.04. What am I doing wrong?
As CBroe pointed out, I was facing the issue because the context of .htaccess has the initial / removed from %{REQUEST_URI} that is usually the conditional part of RewriteRule. Therefore, the solution was only having to change the RewriteRule to:
RewriteRule ^(.*).html$ /index.html?origin=%{REQUEST_URI} [R=301,L,NC]

Redirect URL (including port) with params using .htaccess

I am trying to redirect from
http://www.example.com:81/my/api/search?query=test
to
http://www.example.com:81/my/php/api.php?query=test
using
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule ^api/search(.*)$ /php/api.php?%1 [L]
However, it doesn't work for me. Additionaly for testing htaccess rules I use http://htaccess.madewithlove.be/.
Possibly even better would be to check if request starts from ^api and it's GET type (I guess using RewriteCond %{REQUEST_METHOD}) then redirect to /my/php/api.php?[query params here]
Anyone can point me into right direction?
If your .htaccess is located in /my/ directory then you can use:
RewriteEngine On
RewriteBase /my/
RewriteCond %{QUERY_STRING} ^query=.
RewriteRule ^api/search/?$ php/api.php [L]
QUERY_STRING will be automatically carried over to target URL.

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]

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