301 redirect for all get request - apache

I need to redirect all incoming requests get. for example: site.com?anystr to site.com
I tried to do so
RedirectMatch /?(.*)$ site.com
But it causes cyclic forwarding and i get browser error

RedirectMatch does match anything behind the domain name and before the query string. The regex /?(.*)$ will match any request.
You will need to use mod_rewrite. Enable mod_rewrite in the main config file of Apache and restart Apache. Then add the following to your .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ - [QSD,R,L]
Change the [R] flag to [R=301] after testing this works as expected.

I believe this is hat you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ /? [L,R=302]
i.e. any URL with query string is redirected to root with query string stripped off using ? in the target.

Related

RewriteCond and rules except some page

Hello I want use redirect module on apache2
if request "test.net/xxx" then redirect to "test.test..net/xxx"
but except index.html page
for example if request "test.net/index.html" then no redirect
i wrote the apache2 conf file as below.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.net [NC]
RewriteRule ^http://test\.net/([^index\.html]) http://test.test.net/$1 [R=301,L]
but doesn't work
how to edit above sentences? please tell me
thanks!
You add a condition to take care that the rule is only applied if the exception is not met and fix the actual rewriting rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteRule ^/?(.*)$ http://test.test.net/$1 [R=301]
That rule will work in the http servers host configuration but also in dynamic configuration files (".htaccess").
If you receive an http status 500 ("internal server error") with above rule then chances are that you operate a very old version of the apachE http server. The error log files will then point out an unknown flag [END]. Replace it with the old [L] flag in that case.

redirect https://domain to https://www.domain in httpd24

I'm trying to redirect URL responses from https://domain.com to https://www.domain.com in the apache24 configuration, I'm using Redhat Server.
I have tried to create this Rewrite Condition but its not working
RewriteCond %{HTTP_HOST} ^(domain\.com)?$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
I have fixed the Issue by using Directive IF Condition powered by Apache2.4
I have added this code to my ssl.conf file where it redirected all requests sent to https:// domain .com to https:// www .domain .com . Add this code before the Tag :
<If "%{HTTP_HOST} != 'www.domain.com'">
Redirect "/" "https://www.domain.com/"
</If>
Thank you all for your comments and trying to fix my issue, really its something I appreciate it. Please take a look # for more information about httpd24 directives https://httpd.apache.org/docs/2.4/mod/directives.html
() is a capturing group. You don't want that when you check HTTP_HOST unless you're going to use it as part of the new URL. Also make sure to add the ^ (start of string) and $ (end of string) on your RewriteRule.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
You can test your htaccess here http://htaccess.mwl.be/ without having to upload and refresh your site.

ModRewrite: QUERY_STRING not redirecting

I have the following rewrite rules in place:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^manufacturer=&
RewriteRule (.*) $1? [R=410,L]
But it doesn't seem to work. I'm trying to redirect with URL's like
http://www.example.com/?manufacturer=someone to http://www.example.com/ and page.php?manufacturer=someone to page.php
What am I doing wrong?
EDIT:
I have mod_rewite enabled and AllowOverride All in my site config
The above code is in the .htaccess file
The query string ^manufacturer=& only matches manufacturer parameters that are empty and it also requires that the equals sign is followed by an ampersand which doesn't seem to be your wanted behaviour.
By the way, you do realize the status code 410 means that the resource is Gone (indicates that the resource requested is no longer available and will not be available again)?
You seem to want to redirect back to the current page without the query parameter, so I suggest using a 302 or 301 redirect instead.
Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^manufacturer=.+$
RewriteRule .* $0? [R=301,L]

add alias in front of each URL mod_rewrite apache

If any likes come in the format
http://localhost/index.php/bla...
I want to convert it to http://localhost/er/index.php/bla...
I'm trying the following but it seems to be looping the url indefinitely
RewriteRule ^localhost/index/php/(.*)$ localhost/er/index.php/$1 [R=301,L]
RedirectMatch 301 ^/localhost/index.php/(.*)$ localhost/er/index.php/$1
You've got 2 different things going on, a RewriteRule (mod_rewrite) and a RedirectMatch (mod_alias). You'll only need one, but neither of those can match against the hostname (localhost). If this has to only be limited to the "localhost" host, then you need to do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} localhost$ [NC]
RewriteRule ^/?index\.php/(.*)$ /er/index.php/$1 [R=301,L]
Otherwise, you can just stick with mod_alias:
Redirect 301 /index.php/ /er/index.php/
Everything after /index.php/ will automatically get appended.

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