Simple RewriteRule doesn't change URL - apache

The thing I ask for is truly simple. I want to get from this:
http://test.com/index.php?articles=mynewarticle&id=5
to this:
http://test.com/mynewarticle.html
and I don't understand why, but the following .htaccess is not doing the job:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /index.php?articles=$1&id=5 [L]
When I type in
http://test.com/index.php?articles=mynewarticle&id=5
I get
http://test.com/index.php?articles=mynewarticle&id=5
Not sure if something is modified in Apache in the way that disables this option for me and I don't know where to check this. Thanks in advance for any of the help provided.

You need a combination of external and internal RewriteRules to convert orignal url into SEO friendly format
RewriteEngine on
#redirect "/index.php?articles=foobar&id=123" to "/foobar.html"
RewriteCond %{THE_REQUEST} /index\.php\?articles=([^&]+)&id=.+ [NC]
RewriteRule ^ /%1.html [NE,L,R]
#rewrite "/foobar.html" to "/index.php?articles=foobar&id=5
RewriteRule ^([^/]*)\.html$ /index.php?articles=$1&id=5 [L]

Related

Can I use HTACCESS to format this URL?

I would like to know if it is possible to format a URL like https://site1.com/en/article-details.test-article?item=test to https://site1.com/en/article-details/test-article?item=test so basically changing the .test-article to /test-article.
Because of how my site works it only recognizes .test-article as a valid selector so reading up https://httpd.apache.org/docs/2.4/rewrite/flags.html it seems I may need to use a PT flag to show the user the desired URL but pass the correct one to my site.
So far I have this to replace the dot for a slash
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([a-z]{2})(/?)$
RewriteRule ^/([a-z]{2})/.* https://%{SERVER_NAME}/$1/$2 [L,NC,R=301]
Edit(this works to format the URL as per RavinderSingh13's suggestion)
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([^?]*)(/?)$
RewriteRule ^ https://%{SERVER_NAME}/%1/article-details/%2 [QSA,R=301,NE,L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.(test-article)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]
As per OP's comments adding more Generic rules here:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.([^?]*)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]
You may try this rule as your topmost rule:
RewriteEngine On
RewriteRule ^(.+)\.(test-article/?)$ /$1/$2 [R=302,NE,L,NC]
Change 302 to 301 after testing this rule.

.htaccess - mod-rewrite and redirecting profile URLs with parameters to clean ones

I have links to user profiles like:
https://example.com/chat/index.php?action=user&member=547
https://example.com/chat/index.php?action=user&member=11540
etc.
My goal is to mod-rewrite such URLs so that they look nicer, ie. they should look like:
https://example.com/chat/member-547/
https://example.com/chat/member-11540/
etc.
And URLs without a trailing slash, ie.
https://example.com/chat/member-547
should be 301-forwarded to one with slash, ie.
https://example.com/chat/member-547/
So in .htaccess I tried this, but only the first line seems to work and it's not complete:
RewriteRule ^chat/member-([0-9]+)/$ ./index.php?action=user&member=$1
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
TO SUMMARIZE:
When someone enters URL like:
https://example.com/chat/index.php?action=user&member=547
it should be 301-redirected to:
https://example.com/chat/member-547/
When someone enters:
https://example.com/chat/member-547
it should also be 301-redirected to:
https://example.com/chat/member-547/
I hope there's an efficient way to do it right.
Starkeen's answer is correct, and will work. I'll just suggest an alternative, which acts on the raw request variable itself:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /chat/index\.php\?action=user&(member)=(\d+) [NC]
RewriteRule ^chat/index\.php$ /chat/%1-%2/? [R=301,L]
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
RewriteRule ^chat/(member)-(\d+)/$ /chat/index.php?action=user&$1=$2 [L]
You can use the following rule :
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^action=([^&]+)&member=(.+)$
RewriteRule /chat/index\.php$ /chat/member-%2/? [L,R=301]
RewriteRule ^chat/member-(.+)/?$ /chat/index.php?action=user&member=$1 [L]

Rewrite urls friendly with htaccess. how I can use it on my site?

I'm rewriting urls on htaccess using rewrite rules, but when I want that my site uses it I get stuck.
I want my links looks like:
mysite.com/section/this-is-the-title-of-this-section
I used rewrite rules to get this:
mysite.com/section/?id=longalfanum to mysite.com/section/longalfanum
that was cool for a moment. but I checked some few websites for the URLs I realize that they have friendly URL from the beginning
so I changed the url on the to looks like:
mysite.com/section/longalfanum-the-title-of-the-section
now my links doesn't work like before.
my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#this is for avoid extra /
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule .*/mysite.com/%{REQUEST_URI}? [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#this is my rule
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
obviosly this doesn't work. am I missing a step?
Change
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
to
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
so it matches anything (.*) after 'section/', not just digits (\d+)
Also, you should add an [L] flag to all RewriteRules that don't currently have it to make things a little bit more efficient.
my html with php looks like this:
in the web browser looks like:
mysite.com/section/?idN=alfanum
my rule looks like:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
with this I only get URL like mysite/section/nosensealfanum
it is possible to get the title of new by the htaccess? or I its need to make changes in the way I get the new from the db?
thanks

Using .htaccess to remove unwanted parts of a URL

To someone who knows what they're doing - tho will be really easy. all I want to do is remove a section of my urls, i assume that this is the easiest way.
This is how my URLs currently look:
/blog/?action=viewArticle&url=$postTile
I want to remove:
?action=viewArticle&url=
So that I end up with something like:
/blog/$postTitle
I've tried the below, but I've had no joy:
RewriteEngine on
RewriteRule ^/bwc/(blog)/(.*)/$ /bwc/blog/?action=viewArticle&url=$2
Please help - I think I need to utilise MOD_REWRITE, but I'm not too sure how.
I am not that sure but you please give a try with this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*)$ http://your_domain.com/blog/DateTimestamps-of-news-posts$1 [R=301,L]
See what comes with... :)
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /site/blog/\?action=viewArticle&url=([^&\ ]+)
RewriteRule ^ /site/blog/%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^site/blog/([^/])+$ /site/blog/?action=viewArticle&url=$1 [L,QSA]
These rules need to be in the htaccess file in your document root.

Rewrite URL with subdirectory and file

i am trying to use .htaccess to rewrite the URLs for my site, here is what i would like
currently, the urls look like this
http://www.mysite.biz/store/cart.php?m=new_arrivals
All of the sites are loaded using php GET and the only thing that ever changes is the query string after cart.php so i would like to rewrite to....
http://www.mysite.biz/cart.php?m=new_arrivals
I have been searching all over the internet and have tried out various techniques to do this and none of them have worked. i've tried this...
RewriteRule ^store/(.*)$ http://www.mysite.biz/$1 [R=301,L]
this...
RewriteCond %{REQUEST_URI} ^/store($|/)
RewriteRule ^.*$ /store/cart.php [L]
and this...
RewriteCond $1 !^store
RewriteRule ^(.*) /store/$1 [L]
ive been developing web for a while now but this is my first time really doing anything with .htaccess and i am totally stumped. any help will be greatly appreciated
Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+store/(cart\.php[^\s]+) [NC]
RewriteRule ^ /%1 [R=302,L]
RewriteRule ^(cart\.php)$ /store/$1 [L,NC]