HTACCESS Rewrite rules - .htaccess

I need help to redirect the following URL (in htaccess)
http:www.domain.com/article/the-bp-oil-spill-one-year-later/19918396/20110420/
To
http:www.domain.com/article/the-bp-oil-spill-one-year-later/19918396/2011/04/20/
my original rewrite is:
RewriteRule ^article/([^/]+)/([0-999999999]+)/([0-99999999]+)/?$ /index.php?a=show-post&slug=$1&articleid=$2&date=$3 [QSA,L]
and I am trying to do away with this to a pattern which is like:
domain/2011/04/20/article-name-here/
Appreciate your help on this.
Thanks,
L

Are you after something like this:
URL Example: blah/20080101/
RewriteRule ^blah/([0-9]{0,4})([0-9]{0,2})([0-9]{0,2})/$ /blah/$1/$2/$3/
Would output:
/blah/2008/01/01/

Current/Old: /article/the-bp-oil-spill-one-year-later/19918396/20110420/
RewriteRule ^article/([^/]+)/([0-999999999]+)/([0-99999999]+)/?$ /index.php?a=show-post&slug=$1&articleid=$2&date=$3 [QSA,L]
Desired/New: 2011/04/20/article-name-here/
RewriteRule ^article/([^/]+)/([^/]+)/([0-9]{0,4})([0-9]{0,2})([0-9]{0,2})/?$ /$3/$4/$5/$1 [R=301,QSA,L]
This will allow existing links to your current/old style urls to be redirected (permanently) to your new style urls. This assumes that your php app doesn't need to also have the articleid passed to it - note that articleid is not in your new/desired URL.

Related

How to add a rewrite rule to remove paramaters in htaccess? [duplicate]

I Want to rewrite the url using .htaccess
Plz Read the code, and you well Get to know What I mean
My URL :
article.php?id=1&title=example
Using this in .htaccess
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$
article.php?id=$1&title=$2 [NC,L]
I get
article/1/example
What i need is
article/example
So something like this:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ article.php?title=$1 [NC,L]

How do i URL rewrite using .htacess

i need some help rewriting URL using .htaccess my urls looks like:
http://www.example.com/testro2/product.php?id=426834&productStore=396
And i need have it work with text into it, example:
http://www.example.com/testro2/product/{anythinghere}/id=4268334&productStore=369
or you can make it look more clear like removing id= and &productStore= will be great, else i can use with them. example
http://www.example.com/testro2/product/{anythinghere}/4268334/396
The thing is that i need to have my keyword anythinghere in the URL.
Also, i would like to rewrite
http://www.example.com/testro2/search.php?q=shoes
TO
http://www.example.com/testro2/search/shoes
You can put this code in your /testro2/.htaccess
RewriteEngine On
RewriteBase /testro2/
RewriteRule ^product/.+/(\d+)/(\d+)$ product.php?id=$1&productStore=$2 [L]
RewriteRule ^search/(.+)$ search.php?q=$1 [L]

SEO Friendly URL Command in .htaccess

I have a dynamic page that looks like the following:
www.sitedomain.com/page.php?id=30&name=about_our_company
I want to make my website links SEO friendly by having something like the following:
www.sitedomain.com/about_our_company.html
Or
www.sitedomain.com/about_our_company
My question is: what regex/code I should have in the .htaccess file?
Thanks
This ofc has a fixed id of 30.
RewriteRule ^about_our_company/?$ /page.php?name=$1&id=30 [NC,L]
since /about_our_company doesn't include the ID, it's impossible to invent the ID correctly.
the way I do it in my own CMS is to have something like this in the .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
then in index.php, use the $_REQUEST['page'] (assuming PHP) variable to find the right page details in the database
You could improve this to be more generic:
RewriteRule ^([^/]+)/([^/]+)?$ /page.php?id=$1&name=$2 [NC,L]
The following URL's would all match:
http://example.com/30/about_our_company
http://example.com/29/contact
http://example.com/10/our_work

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

.htaccess: how to only rewrite .php URL?

My rewriterule with a condition is working fine as below:
http://www.sitename.com/index.php?n=text redirects to
http://www.sitename.com/pages/text
and the page renders properly, however, there is a problem that with the redirected URL the arguments are also added to the URL. So actually in address bar it looks like-
http://www.sitename.com/pages/text?n=text
Could anyone help me on this? The htaccess code is given below.
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php http://www.sitename.com/pages/%1 [r=301,nc]
You probably want to catch "index.php.*". Otherwise mod_rewrite only replaces the "index.php" part of the URL "index.php?n=text" with the new URL.
Guss,
From what you suggested, i reconstructed it as follows:
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php.* http://www.sitename.com/pages/%1 [r=301,nc]
This doesnt seem to be working either. Can you please elaborate on what you have said?
thank you
aditya
donĀ“t use the url in the rewrite rule, apache then sends a http 200 code and then the 301...
try sth. like this:
RewriteRule (index\.php)(?n=)(.*) /pages/$3 [r=301]

Resources