I tried to remove query string from my url on my homepage only.
Because when a visitor come from social, example: Facebook add this argument at the end of the url: ?fb=654616516546... and that breaks my website.
I have tried this but it overwrites all the url arguments on all the pages, I would like this rule to apply only to the homepage:
Options -Indexes
DirectoryIndex /index.php?page=servers
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^([^/]*)=([^/]*)$ [NC]
RewriteRule (.*) /$1? [R=301,L]
</IfModule>
Thank you !
You need to use ^$ instead of (.*) in the Rule's pattern so that your rule applies only to the root URI .
RewriteRule ^$ /$1? [R=301,L]
Related
I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>
I want to rewrite one specific url.
http://example1.com should be http://example2.de .
But http://example1.com/subdir or http://sub.example1.com should remain the same.
I found the following, which successfully rewrites example1.com, but also every url which starts with example1.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Background: I want to redirect the main page of an WP-Multisite but want to make sure that I can work with the backend of wordpress and run other multisites which are subdomains.
For matching only http://example.com domain (without possibility to add anything before or after the example.com) use the following code:
RewriteCond %{HTTP_HOST} ^(example.com(\/{0,1})){1}$
RewriteRule http://example2.de(\/{0,1}) [R=301,L]
That (\/{0,1}) part is for matching both example.com and example.com/ (but nothing esle) - if you do not wish to match example.com/ remove that part from both rows.
You're pretty close but you don't need to capture URI in $1:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^$ http://example2.de/ [L,R=301]
My attempt:
RewriteCond %{QUERY_STRING} ^id=(.*)$ [NC]
RewriteRule ^/product$ /product/%1 [NC,L,R=301]
I want to apply this rule to /product/ and /supplier/ directories only. They are both first level sub directories.
Note: product/?id={xxx} is actually product/index.php?id={xxx}. Apache hides my extensions and indexes. Just want to point that out.
My product/index.php handles the parameter given and determines what page it should show:
index.php
if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) ) {
//html for individual page e.g. /product/?id=foo
//e.g. <h1><?= $_GET['id'] ?> Page</h1>
} else {
//html for product list e.g. /product/ (no parameters)
}
Try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} id=(.+) [NC]
RewriteRule ^(product|supplier)/?$ /$1/%1? [NC,L,R=301]
In .htaccess files the URI-path test it in the rule does not have a leading slash (^/product), therefore the regex cannot have it either. The trailng ? deletes the incoming query.
If the rule-set is to be placed at Apache main configuration files, the leading slash should be kept: ^/(product|supplier)/?$
UPDATE
To show the wanted URL but still get the data from the original URL.
Request: /product/?id=parameter
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(product|supplier)/\?id=([^\s]+) [NC]
# Strip the query and redirect permanently
RewriteRule ^(product|supplier) /$1/%3? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
# Map internally to the original request
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
Another option is to use directly the "pretty" URL in the request:
Request /product/parameter to /product/?id=parameter
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
I would like to redirect mysite.com/index.php to mysite.com/index.php?id_category=12&controller=category
Here is my htaccess file.
But it doesn't work. Chrome says : This webpage has a redirect loop.
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule index\.php http://mysite.com/index.php?id_category=12&controller=category
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://www.mywebsite.com/index.php?foo=bar [R=301,L]
The first condition checks if the URI is equal to index.php and the second one checks if GET values are empty. The AND between the 2 conditions is implicit here.
Probably you should change use another file name instead of index.php for the destiny. But you can try:
RewriteRule index.php index.php?id_category=12&controller=category
I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.