.htaccess - Pretty User Profile URLs - .htaccess

I want to make my user profiles 'pretty' as it were. Here is the regular URL:
user.php?user=UserName
I want this to become:
user/Username
Please help :) Im a .htaccess newbie.

Try these directives:
RewriteEngine on
RewriteRule ^user/([^/]+)$ user.php?user=$1 [L,QSA]
This rule rewrites any request with a URL path of the form /user/foobar internally to /user.php?user=foobar.

RewriteRule ^user/(.+)$ user.php?user=$1 [L,QSA]

Related

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

Part of my .htaccess code is as follows:
RewriteRule ^([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
This basically redirects URL, example.com/XX to example.com/nextlevels_state.php?state=XX and example.com/XX/1 to example.com/nextlevels_state.php?state=XX&page=1 (internally).
Now, I would like to change the URL structure of the URL from example.com/XX to example.com/nextlevels/XX and example.com/XX/1 to example.com/nextlevels/XX/1
and I tried changing .htaccess to as follows:
RewriteRule ^nextlevels/([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
However, as the site urls are already indexed in search engines, I would like to know a way to redirect all the traffic from example.com/XX to example.com/nextlevels/XX (externally) using .htaccess .
Please guide me in this regard. Thank you community :)
Could you please try following, written and tested with shown samples only(improving your already done attempts here). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for redirect to url example.com/nextlevels/XX.
RewriteRule ^([A-Z]+)$ nextlevels/$1 [R=301]
RewriteRule ^nextlevels/([A-Z]+)/?$ nextlevels_state.php?state=$1 [NC,L]
##Rule for redirect to url example.com/nextlevels/XX/1.
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels/$1/$2 [R=301]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC,L]

force index.php in url with htaccess

I've spent the last two hours reading stackoverflow and the apache manual trying to find a solution to this, but haven't found any, if a directory is access (http://domain.ext) I'd like to force index.php to appear in the url (http://domain.ext/index.php) if possible.
If anyone could help I'd really appreciate it, thank you.
RewriteEngine on
RewriteRule index.php
you can use this:
RewriteEngine on
RewriteRule ^$ /index.php?
(but you will not see it in a browser because the rewrite engine just directs to the correct file/directory without changing the url from the browser)
with [R] tag you can redirect the user so the new url would appear in the browser
I've just found what works for me
RewriteEngine On
RewriteRule ^/?$ $1/folder/index.php$2 [R=301,L]

Get the language from URL with .htaccess

I have several languages on my site.
At the root I have a index.php. It's my homepage.
I want this type of url : domain.com/fr/ or domain.com/en/
Witch must be the same as domain.com/index.php?lang=fr or domain.com/index.php?lang=en.
So I use this .htaccess rule:
# Redirect to a page (xxxxx.com/index.php?lang=fr)
RewriteRule ^(fr|en|es|cn|ar)/index$ index.php?lang=$1 [L]
But it don't work.
Could you please help me with that ?
Thanks.
You can use:
RewriteEngine On
RewriteRule ^(fr|en|es|cn|ar)/$ index.php?lang=$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

HTACCESS Rewrite rules

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.

Resources