how to rewrite html url as directory in htaccess - php

I want to rewrite all of my category urls as directories instead of long html pages. How do I do that? (The site is homewetbar.com if you need to look at the directory structure further to understand how it currently works.)
For Example:
First level category:
www.site.com/great-gift-ideas-c-35.html
I would like to display instead as:
www.site.com/great-gift-ideas-c-35
Second level category:
www.site.com/great-gift-ideas-gifts-recipient-c-35_85.html
I would like to display instead as:
www.site.com/great-gift-ideas/gifts-recipient-c-35_85
Third level category:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43.html
I would like to display instead as:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43
OR:
www.site.com/great-gift-ideas/gifts-recipient/groomsmen-gifts-c-35_85_43 (whichever is easier)

I think mod_rewrite is what you are looking for, here is a link with how to set it up and how to start rewriting your urls. It should have enough information for what you are looking for, for more advanced stuff you will want to know more about regular expressions, but your examples are simple enough.
RewriteEngine on
RewriteRule ^great-gift-ideas-c-35.html$ great-gift-ideas-c-35
this should work for your first example, the rest just replace out the appropriate urls.

You would need to rewrite all the href in your site to delete the ".html" part. Then, add this to your .htaccess
RewriteEngine on
RewriteCond ${REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [L]
That would make any URL without the ".html" work as if it had it

Just check out this site: http://www.generateit.net/mod-rewrite/index.php it generate all you need
RewriteEngine On
RewriteRule ^([^/]*)$ /?example=$1 [L]
Changing example to what you need should do it

Related

Handle .htaccess redirects with different parameters?

I would like to redirect urls that don't go into directories to one script (script1.php for example), and urls that have categories to another script (script2.php). Basically I would like to do something like this:
http://www.test.com/user1 -> http://www.test.com/script1.php?username=user1 where script1.php gets the username and presents an appropriate page for that user. I have that part working with this code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ script1.php?username=$1 [L,QSA]
The problem now is that I would also like to have descriptive product urls so that for example http://www.test.com/clothes/jackets/cool-red-jacket-25 redirects to http://www.test.com/items.php?category=clothes&subcategory=jackets&id=25. I have some code that should work for that too:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z-]+)/([a-zA-Z-]+)/.*-A([0-9-.]+)\.php$
script2.php?category=$1&subcategory=$2&id=$3 [L]
The problem I'm having is combining these 2 types of redirection. The first redirect always redirects to its own page and the second redirect will never get reached. Is it possible to combine these 2 and in which way? Basically I need something like this for htaccess if possible
if(!urlHasDirectories) {
redirect to script1.php?username=$username;
} else {
redirect to script2.php?category=$category&subcategory=subcategory&id=$id;
}
Lets go over basic regexs, we'll use Regex101 for this. The . is any character and * is a quantifier of the previous character/grouping zero or more times. So your first regex, RewriteRule ^(.*)$ script1.php?username=$1 [L,QSA] says rewrite anything that starts with anything and ends with anything to scripts1.php. That isn't what you want.
Demo: https://regex101.com/r/hK0xY3/1
With regexs it is best to be as specific as possible.
I would make your rule for users:
RewriteRule ^(user\d+)$ script1.php?username=$1 [L,QSA]
Demo: https://regex101.com/r/hK0xY3/4
The + is another quantifier here meaning one or more, so you could change that to an * if a number ins't required.
You said you wanted a regex that finds if there is a / in the path. I think it is best to tell the regex what to look at but that is possible:
^.+?\/
Demo: https://regex101.com/r/hK0xY3/3
With this approach though any directory request will be redirected...
for seo friendly url use this like :
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^item/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ items.php?category=$1&subcategory=$2&id=$3
You need to put the more specific rule higher in the .htaccess file so it could be matched first. Use this in general.
Be awere that the most general rule (.*) that matches the rest of requests can be used for more different actions.
.com/john-doe // for user profile
.com/my-new-article // for showing article and so on
If you want to combine all those variations u need to decide which action to use (profile or showing article) on your application level. For example controller in mvc architecture. It can be accomplished easily by conditions.
if(userExists($userName))
// showing template for user profile etc...

CMS Pages in Database .htaccess configuration

In my CMS I store all pages in a database. If you request a page currently it works like this:
Requested URI: www.xyz.com/site.html
.htaccess: mod_rewrite creates: /index.php?q=site
index.php: Looks up in Database for the entry site.
To clean up the URLs I like to have URLS like this: www.xyt.com/about/site.html or www.xyt.com/about/groups/groups.html
In my Database is an entry for every Page called owner which represents the Parent Site.
The Problem for me is that the number of 'folders' is not fixed.
So i thought I should Change www.xyt.com/about/site.php to /index.php?q=about-site in the .htaccess and than write a PHP function which finds the site site with the Parent about
What would be the RewriteRule?
I that a good way or is there an other (better) way?
Changing the foo/bar/about/site/etc.html to index.php?q=foo-bar-about-site-etc is much more difficult with mod_rewrite than it is with PHP. Just do this in php, get the $_GET['q'] variable and explode the string into an array or something using the / flags. It's also better this way because you'll know for sure that the / characters are reserved and you won't end up having to resolve stuff like /foo-bar/about/site. The rules would look something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?q=([^&\ ]+)
RewriteRule ^ /%1.html [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /index.php?q=$1 [L]

SEO friendly URL not working using .htaccess

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below
RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1
The actaul URL looks like this
http://localhost/n/article.php?title=this-is-the-first-news-article
but I want it to look like this :
http://localohst/n/article/this-is-the-first-news-article
When I applied the RewiteRule above it does not change to the desired URL
This should do it. You are missing the n. Not sure why you need the word title though.
RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301]. Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]
If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:
RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
it will be RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

Rewrite URL Parameter containing dot

I think that I am trying to achieve an impossible result.
The scenario is PURL-Mailing and I already got some URL's rewritten to fit the URL, sent to the customer.
The customer enters the site by the following domain: http://domain.com/UserName
The Variable UserName represents the GET-Variable, which equivalent to http://domain.com/index.php?user=UserName
I achieve this with the following rewrite Rules:
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 [QSA]
#This works perfect and translates to http://domain.com/UserName
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1 [L]
#This achieves the goal but does not reflect in the URI I want:
#http://domain.com/UserName
To go further, there are also some Names containing a dot in the Name like A.Jackson that also need to be treated as UserName. As those are only 13 Name I could implement them manually. What I don't know is how I can prevent the part after the dot to be handled as a file extension. Is there a way to write a custom handle in *mod_rewrite* for those?
And if so, can anybody explain to me how?
Thanks in advance and best regards!
ok try below
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)[/]*$ test.php?name=$1 [L]
replace 'anyother folders that you want to ignore' with folder name that you want to ignore. Seperate each folders with '|'
You also have to provide full path to the CSS, image or any other links used in your web page when you using URL rewrite functions
Here is your fix
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)$ index.php?name=$1 [L]

Create SEO-friendly URL with PHP with just the Querystring Value

Is there a way use mod_rewrite to produce the result below?
Original URL:
http://www.domain.com/shop.php?id=newyork
to
SEO friendly URL
http://www.domain.com/newyork
I've seen plenty of example where the above URL can be converted to http://www.domain.com/shop/newyork but I actually don't want to display the word 'shop/' so just http://www.domain.com/newyork
I'd have a go with something like the following, off the top of my head
RewriteRule ^([a-zA-Z]*)$ www.example.com/shop.php?id=$1
Do bear in mind that anything after your root domain, will be piped into your shop.php script.
Yes, in your .htaccess file put
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/([^/.]+)$ shop.php?id=$1 [L]
</IfModule>
([^/.]+) will match anything that isn't a / or . and store that info,
$1 at the end outputs that info into your shop script.
[L] tells mod_rewrite to stop looking for rules if this one works.
Based on your example:
RewriteRule ^([a-z]+)$ /shop.php?id=$1 [L]
Would match newyork, alaska, hamburg but not highway-1.

Categories