Rewrite rule always redirect to wrong url - apache

Hello All,
I need to rewrite http://mysite.com/user/profile/following?profile_name=MYNAME to http://mysite.com/user/profile/MYNAME/following
I have written rule like this:
RewriteRule user/profile/(.*)/(.*) /user/profile/$2?profile_name=$1 [L,R=301]
when i put url like http://mysite.com/user/profile/MYNAME/following in the browser it always redirect me to http://mysite.com/user/profile/following?profile_name=MYNAME
What did i miss?
Thanks in Advance

You can use this code:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+user/profile/([^?]+)\?profile_name=([^\s&]+) [NC]
RewriteRule ^ /user/profile/%1/%2? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^user/profile/([^/]+)/([^/]*)/?$ /user/profile/$2?profile_name=$1 [L,NC,QSA]

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]

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]

htaccess rewrite

I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE
I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]

Apache mod_rewrite: can these simple RewriteRule be improved? And suggestions!

I started finally to understand Apache mod_rewrite. It's pretty GREAT!
Plz have a look at the followings:
1) Permanent redirects http://www.domain.com/folder_name/ (with or without final slash and with or without the www) to http://www.domain.com/some/path/some_page.html
RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]
2) Permanent redirects all requests to www.domain.com... to same path and file request but without www in domain
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
They all work as expected and do their jobs, I'm simply curios if some guy, who is more expert than me in mod_rewrite, could give me some advises like: "it could be better in this way...", "there might be a problem if...", etc.
Thanks!
Use the ? quantifier instead of * and you don’t need to escape the substitution URL:
RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
You might want to consider HTTP 1.0 requests where the Host header field is missing. Another useful extension would be to take HTTPS into account:
RewriteCond %{HTTP_HOST} !^(|example\.com)$
RewriteCond %{HTTPS} ^on(s)|
RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]