Redirecting All Requests to Index.html - apache

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]

Related

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

.htaccess redirect from directory to subdomain without changing url

I would like to redirect /blog/ to another project. Without changing the url.
The hole thing almost works with the redirect rules below and using Apache mod_proxy, but only if it hits the index itself on /blog/ it DOES redirect to blog.website.nl.
RewriteCond %{HTTP_HOST} ^website\.nl [NC]
RewriteRule ^(.*)$ http://blog.website.nl/$1 [P,L]
When I visit website.nl/blog/category/test/
it works fine!
So why is this still changing the url on /blog/ and how to fix that?
Thanks! This has been quite a puzzle for me so far.
Update:
I managed to get it working. Instead of adding a .htaccess file to blog folder I added this code to the root /htaccess file. Now it all works!
RewriteCond %{HTTP_HOST} ^website\.nl[NC]
RewriteRule ^/?blog/(.*)$ http://blog.website.nl/$1 [P,L]

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.

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