Proper mod_rewrite for SEO friendly URLs - apache

I have a custom php script, each having a page for page.php?id=[number] and page.php. I've read a few .htaccess tutorials but I don't know which one is the most appropriate way of doing it:
Example,
page.php?id=12 to page/id-12/and
page.php to page/ AND page
Here's what I currently have:
RewriteEngine On
RewriteRule ^category/([0-9])$ /category.php?id=$1
RewriteRule ^category/$ /category.php
Code above returns 404 not found if i access category but appears okay if i access category/ (with a slash)

All your rules include the slash / at the end of category, thus of course category cannot match it.
RewriteRule ^category/?$ /category.php
The ? after / makes it optional

Related

.htaccess : Pretty URL with whatever number+names of parameters

Hello !
I know there already are a lot of topics about URL rewritting and I honestly swear I've spent a lot of time trying to apply them to my problem but I can't see any of them perfectly applying to my situation (if you find otherwise, please give the link).
-----
Here's the problem :
I'm learning MVC model and URL rewriting and I have my URL like this :
http://localhost/blahblahblah/mywebsite/index.php?param1=value1&param2=value2&param3=value3 ... etc ...
What I want (for some MVC template goals) is to have this kind of URL :
http://localhost/blahblahblah/mywebsite/value1/value2/value3 ... etc ...
-----
Whatever are the names of the parameters and whatever are the values.
This is the most essential thing I can't find a solution for.
(Also don't mind the localhost blahblahblah, this has to work even on distant websites but I trust it will work fine on online website has this part of URL may have no importance in what I want to do)
Thanks a lot for your time if you can help me seeing clearer in what I need to do.
If the .htaccess file is located in the document root (ie. effectively at http://localhost/.htaccess) then you would need to do something like the following using mod_rewrite:
RewriteEngine On
RewriteRule ^(blahblahblah/mywebsite)/(\w+)$ $1/index.php?param1=$2 [L]
RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)$ $1/index.php?param1=$2&param2=$3 [L]
RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)/(\w+)$ $1/index.php?param1=$2&param2=$3&param3=$4 [L]
# etc.
Where $n is a backreference to the corresponding captured group in the preceding RewriteRule pattern (1st argument).
UDPATE: \w is a shorthand character class that matches a-z, A-Z, 0-9 and _ (underscore).
A new directive is required for every number of parameters. You could combine them into a single (complex) directive but you would have lots of empty parameters when only a few parameters were passed (rather than not passing those parameters at all).
I'm assuming your URLs do not end in a trailing slash.
If, however, the .htaccess file is located in the /blahblahblah/mywebsite directory then then directives could be simplified a bit:
RewriteRule ^(\w+)$ index.php?param1=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?param1=$1&param2=$2 [L]
RewriteRule ^(\w+)/([\w]+)/([\w]+)$ index.php?param1=$1&param2=$2&param3=$3 [L]
# etc.
Don't use URL parameters (alternative method)
An alternative approach is to not convert the path segments into URL parameters in .htaccess and instead just pass everything to index.php and let your PHP script split the URL into parameters. This allows for any number of parameters.
For example, your .htaccess file then becomes rather more simple:
RewriteRule ^\w+(/\w+)*$ index.php [L]
(This assumes the .htaccess file is located in /blahblahblah/mywebsite directory, otherwise you need to add the necessary directory prefix as above.)
The RewriteRule pattern simply validates the request URL is of the form /value1 or /value1/value2 or /value1/value2/value3 etc. And the request is rewritten to index.php (the front-controller) to handle everything.
In index.php you then examine $_SERVER['REQUEST_URI'] and parse the requested URL.

Rewriting directory into a query parameter in htaccess

I converted a website I'm building into a web view app in iOs.
I would like to track visitors that use the app instead of the website by adding a directory to my URL.
For instance, the "about" page URL would go from "https://example.com/about/" to "https://example.com/app-ios/about/"
My question is how to write an htaccess rule that tells my server to go to the path "/about/" and skip the "/app-ios/" directory?
Also, I'd like to add ?app=app-ios in my query parameters.
The most promising thing I found was this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/node/(.+)$ /search/node/?app=$1 [QSA,NC,L]
But I'd need to specify what comes before "foo".. In my case, "app-ios" is at the beginning of the request uri, always.
Plus I don't want a redirection. I just want my server to read /app-ios/something/other-thing/ as /something/other-thing/?app=ios.
First one question for tracking I would recommend to make this over a Query Parameter (as you wrote) like ?client=ios, but this might be an own opinion.
For rewriting the URL you could do the following:
to remove from the IOS from the URL (not tested):
RewriteEngine On
RewriteRule (.*)ios/(.*) $1/$2?client_id=ios&%{QUERY_STRING} [L]

Need to create a RewriteRule in .htaccess for a Magento website

I am currently trying to put a RewriteRule into my .htaccess file for my Magento website that will allow me to write a category URL in the following way:
http://mydomain.com/dir/<direction>/order/<order>/<Magento category URL path>.html
What I am basically looking to do is use my robots.txt file to make some of the category URLs not appear (specifically when you apply a different sort order to the category).
So let's assume I have the following URL:
http://mydomain.com/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
I would like that to be rendered just as if it the URL was:
http://mydomain.com/footwear/mens-work-boots/motorcycle-boots.html?dir=asc&order=sales_index
The code I have put in my .htaccess file is as follows:
RewriteRule ^dir/(.*?)/order/(.*?)/(.*?)$ $3.html?dir=$1&order=$2
For some reason, when I have this in there, I get a 404 error. Can someone point me in the right direction to make this work for me?
I tried with this on my server
RewriteRule ^(.*)/dir/(.*?)/order/(.*?)/(.*?)$ $4.html?dir=$2&order=$3 [R,L]
and when i issue request
http://myserver/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
i get proper redirection to
http://yuave.dev:81/footwear/mens-work-boots/motorcycle-boots.html.html?dir=asc&order=sales_index
May be you are missing [L] flag on your request.

Rewrite To WordPress Page

I have an existing page of /programs/kids.php that I want to load a category page from WP. I want the .htaccess file in /programs to handle this rewriting for me.
Something along the lines of:
RewriteEngine on
ReWrite kids2.php http://www.mysite.com/blog/cat/kids/
Any help would be awesome.
If your sure mod_rewrite is enabled by apache, the it is simple as
RewriteEngine on
RewriteRule kids.php http://www.mysite.com/blog/cat/kids/
or
RewriteEngine on
RewriteRule ^programs/kids.php$ http://www.mysite.com/blog/cat/kids/
depending on location and strictness (^hhh$ matches the whole string)
The directive is named RewriteRule and not just Rewrite. So try this:
RewriteRule ^programs/kids2\.php$ /blog/cat/kids/
But if you want have requests to /blog/cat/kids/ rewritten internally to /programs/kids2.php (the exact opposite of what you’ve mentioned), try this rule:
RewriteRule ^blog/cat/kids/$ programs/kids.php [L]
You could also do an include for that kids.php in the content of an existing WP page, but you'd need the ExecPHP plugin installed.
Then kids.php would display as styled content inside a WordPress page. Which is sometimes desirable, from a site cohesiveness standpoint.
Something like this:
<?php require_once(ABSPATH. '/programs/kids.php');?>
Not exactly what you asked for, but maybe an alternative approach.

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.