I have this line of code:
RewriteRule ^account/?edit=([A-Za-z]+)$ /?goTo=account&act=edit_$1 [L,NC]
When I go to mysite.com/account/?edit=username it is supposed to refer to mysite.com?goTo=account&act=edit_username but it gives me error 404
Any help?
Thanks!
You cannot match QUERY_STRING using RewriteRule. That requires a RewriteCond like this:
This should work:
RewriteCond %{QUERY_STRING} (?:^|&)edit=([^&]*) [NC]
RewriteRule ^account/?$ /?goTo=account&act=edit_%1#something [L,NC,NE,QSA]
Reference: Apache mod_rewrite Introduction
Related
So I'm trying to write a rule that will respond with a 404 if certain strings are passed to any of the php scripts. Here's with what I came up with:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^$ [R=404,L]
That rule appears to be matching only www.domain.com/?string1 or www.domain.com/?String2, but not www.domain.com/whatever.php?var=string1 or www.domain.com/directory/script.php?var=string1 or www.domain.com/directory/1/script.php?var=string1 and so on.
Can anyone help and point out what I am doing wrong?
Best,
-Iulian
Your RewriteRule is requiring an empty path. Try it like this:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^.*$ - [NC,L]
As Kevin says, you are requiring an empty URL before the query string, with ^$. You don't need all the .*, you don't have to match the full string. This will work, you don't need the RewriteBase either:
RewriteCond %{QUERY_STRING} (?:string1|string2) [NC]
RewriteRule ^ - [R=404,L]
The ?: just says don't capture this, it's only for grouping. The ^ is a way of matching anything. The - says don't change the URL.
I'm trying to rewrite my website URLs. I have:
http:// website.com/v2/message.php?ID2=123
I want:
http:// website.com/v2/message-123.php
I tried something in my htaccess but I've a redirection loop :'(
Here is my .htaccess:
RewriteCond %{QUERY_STRING} ^ID2=([0-9]+)$
RewriteRule message.php? http://website.com/v2/message-%1.php? [L,R=301]
Can someone help me with this?
I suggest not using .php in pretty URL, make it extension-less. Try these rules in v2/.htaccess:
RewriteEngine On
RewriteBase /v2/
RewriteCond %{THE_REQUEST} /message\.php\?ID2=([^&\s]+) [NC]
RewriteRule ^ message-%1? [NE,R=302,L]
RewriteRule ^message-(.+)/?$ message.php?ID2=$1 [L,QSA,NC]
This was kind of hard to search for, I found examples of other work but basically I have a url:
site.com/?page=test&id=3
I want to rewrite as: site.com/test:3
with 'test' being page, and 3 being id.
I have the following match for equal values:
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ /%1:%2?
But that only works for the key and the value, what regex can I use to select just the key, with %1 and %2 ?
Thanks,
My Second Attempt:
(I only need on index.php)
RewriteCond %{QUERY_STRING} ^page=(\w+)&id=([0-9]*)$
RewriteRule ^index\.php$ /%1:%2**?** [R=302,L]
but now a 404 error is occuring when I do: index.php/details:1
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?test=([^\s&]+)&test2=([^\s&]+) [NC]
RewriteRule ^/?$ /%1:%2? [R=302,L,NE]
RewriteRule ^([^:]+):([^/]+)/?$ /?test=$1&test2=$2 [L,QSA]
So I have been struggling on finding the rule to match this rewrite. I am working on a client website and it is a nightmare with the number of duplicate title tags. I have managed to resolve most of them by enforcing forward slash, redirect non www. to the www. version and disallow crawling of https version of the website.
The issue I am having at the moment. I have over 1000 URLs that are duplicate content, each product has two different URLs with the exact same content. An example is:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
http://www.example.co.uk/product/widget2/
http://www.example.co.uk/widget2/
Now the following URLs have the same content:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
I want to redirect any URL that contains "/product/" to the URL version without "/product/" in the URL if that makes sense. I honestly don't know where to start and would really appreciate the help.
Thanks in advance
EDIT: The recommended rule:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
does not work. It may be conflicting. These are the other rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^/product/(.*)$ /$1 [R=301]
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I dont know if there are any conflicts here. Please help
Have your full .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1? [L,R=301]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [L,R=301]
RewriteRule ^product/([^/]+)/?$ /$1/ [R=301,L]
Assuming the URLs always start with product, this should work:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
It'll need to go in your main site conf or .htaccess
Trying to make a redirection URL in htaccess.
I want to redirect URLs like
www.domain.com/pageANYTHING
to
www.domain.com
But I have an exception : when I got this
www.domain.com/page.phpANYTHING
do nothing.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\.phpANYTHING [NC]
RewriteRule ANYTHING$ http://www.domain.com/? [L,R,NC]
Try this:
RewriteEngine On
RewriteRule ^page(?!\.php).*$ / [R=301,L]
In fact It works with a mix of your two propositions.
RewriteCond %{REQUEST_URI} !\.(?:php)$
RewriteRule ^page(?!\.php).*$ / [R=301,L]
Thank you so much guys.