How to rewriting the URL using .htaccess - regex

How to rewrite a URL from localhost/folder/abc.php?p_id=1 to localhost/folder/xyx/1.
I tried lots of methods but still couldn't rewrite the URL.
RewriteEngine On
RewriteRule ^xyx/(.+)$ folder/abc.php?p_id=$1 [L]

You can use this rule inside folder/.htaccess:
RewriteEngine On
RewriteRule ^xyz/(.+\.(?:css|js))$ /folder/$1 [L,R=301,NE,NC]
RewriteRule ^xyz/(.+) abc.php?p_id=$1 [L,QSA,NC]

Related

Apache RewriteEngine

I want to rewrite an URL like this
http://domain1.com/some_directory/12345678?x=1/audio.mp3
to an URL like this http://domain2.com/12345678?x=1.
I tried this rewrite rule RewriteRule /some_directory/(.*)$/(.*).mp3 http://domain2.com/$1 [NC,L] but it doesn't work.
You can use this rule in root .htaccess by matching query string in a RewriteCond:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)(x=\d+)[^.]+\.mp3 [NC]
RewriteRule ^some_directory/(.*)$ http://domain2.com/$1?%1 [NC,L,R=302,NE]

Redirection loop (URL rewriting)

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]

Redirect urls with parameters to homepage url

I have the following example urls that i need redirecting to www.example.co.uk
http://www.example.co.uk/wordpress/?feed=comments-rss2
http://www.example.co.uk/wordpress/?feed=rss2
http://www.example.co.uk/wordpress/?cat=518
http://www.example.co.uk/wordpress/?cat=514
http://www.example.co.uk/wordpress/?cat=520
http://www.example.co.uk/wordpress/?cat=521
http://www.example.co.uk/wordpress/?cat=523
http://www.example.co.uk/wordpress/?cat=515
http://www.example.co.uk/wordpress/?p=119
There are many more urls further to this list that all have a wordpress/ notation.
Would I have to write a conditional rewrite for each like this:
RewriteCond %{QUERY_STRING} ^feed=comments-rss2$
RewriteRule ^/wordpress/?(.*)$ http://www.example.co.uk [R=301,L]
RewriteCond %{QUERY_STRING} ^cat=518$
RewriteRule ^/wordpress/?(.*)$ http://www.example.co.uk [R=301,L]
or is there a rule i can write that redirect all urls that have /wordpress in them to redirect to www.example.co.uk
I believe a single rule like would be suffice:
RewriteCond %{QUERY_STRING} (^|&)(feed|p|cat)=[^&]+ [NC]
RewriteRule ^wordpress(/.*)?$ http://www.example.co.uk/? [R=301,L,NC]
Make sure this is very first rule in your .htaccess just below RewriteEngine On line.

How to rewrite this URL using .htaccess

I am basically just trying to write a .htaccess redirect that turns the url
www.example.com/Artist/Profile?Artist='test' to www.example.com/Artist/test
without actualy redirecting the user.
use the following directive
RewriteCond %{QUERY_STRING} (^|&)artist='([^&]+)' [NC]
RewriteRule ^Artist/Profile$ /Artist/%2? [L,NC]

.htaccess mod_rewrite... I think?

I was wondering how in .htaccess I can redirect the following URL:
/mysite.com/blog/Something => /mysite.com/blog.php?tag=Something
Here is a script I've used on a different site however this is a bit simpilar:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?name=$1
</IfModule>
Thanks in Advance
Just add this rule:
RewriteRule ^blog/(.*)$ blog.php?tag=$1 [L,QSA,NC]
Update: As per your comment this is the rule you will need:
RewriteRule ^blog/([^/]*) blog.php?tag=$1 [L,QSA,NC]
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#/mysite.com/blog/Something to /mysite.com/blog.php?tag=Something
RewriteRule ^blog/(something)$ blog.php?tag=$1 [L,NC,R=301]
If something is a variable then change the rule to
RewriteRule ^blog/(\w+)$ blog.php?tag=$1 [L,NC,R=301]
If you want the URL to stay the same in the Users browser then drop the R=301 as below
RewriteRule ^blog/(\w+)$ blog.php?tag=$1 [L,NC]